0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】PCのバッテリー状態を簡単に確認したい【Windows バッテリーレポート】

Last updated at Posted at 2024-12-23

ノートパソコンのバッテリーの劣化度合いを簡単に確認する方法を調べていたら、どうやらWindows OSには標準でバッテリーレポートの機能が存在するらしい。
個人的に知りたいバッテリーの最大容量が50%を下回っているかは標準機能をそのまま使うよりもあるQiita記事に記載のあったスクリプトを使うと良さそうなので簡単な備忘録として残す。

以下のQiita記事のスクリプト参照。

何故かコメントアウトの日本語でエラーが出るため面倒だがGoogle翻訳にかけ英語で記載した。
また、コードの後半部分は少しだけ変更を加えバッテリーの最大容量が50%を下回っているとWARNINGが表示されるようにし任意でバッテリーレポートのページをブラウザで開くようにしている。

battery_report.ps1
# ------------------------------------------------------------------------------
# This script is checking for battery deterioration level of laptop.
# ------------------------------------------------------------------------------

# Create XML file to use for later what is the results of the powercfg commands.
$TEMP_FILE = './battery_report.xml'

# Execute the powercfg command and obtain the execution results in XML format.
# The messages generated when the powercfg command is executed are piped to Out-Null so that they are not output to standard output.
Write-Host 'Checking for the battery deterioration level of laptop...'
powercfg /batteryreport /xml /output $TEMP_FILE | Out-Null

# Parse the XML and get the DESIGN CAPACITY and FULL CHARGE CAPACITY.
# Get it by using Batteries.FirstChild because assume there is only one battery.
$battery_xml = [xml](Get-Content $TEMP_FILE)
$design_capacity = $battery_xml.BatteryReport.Batteries.FirstChild.DesignCapacity
$full_charge_capacity = $battery_xml.BatteryReport.Batteries.FirstChild.FullChargeCapacity

# Calculate the capacity attenuation rate then align to one decimal place.
$attenuation_rate = ($design_capacity - $full_charge_capacity) / $design_capacity * 100
$attenuation_rate = [Math]::Truncate($attenuation_rate * 10) / 10; # Align to one decimal place.

# Delete the file resulting from executing the powercfg command.
Remove-Item -Path $TEMP_FILE

# Output the capacity attenuation rate of this battery (%).
Write-Host "CAPACITY ATTENUATION RATE: $attenuation_rate %"
Write-Host "DESIGN CAPACITY: $design_capacity mWh"
Write-Host "FULL CHARGE CAPACITY: $full_charge_capacity mWh"

# Show warning massage to replace battery if capacity attenuation rate is over 50%.
if ($attenuation_rate -gt 50)
{
	Write-Host "[" -NoNewline
	Write-Host "WARNING" -NoNewline -ForegroundColor Red
	Write-Host "] " -NoNewline
    Write-Host "Please replace to new battery."
}

$question = Read-Host 'Do you want to check the battery report? (yes/no)'
if($question -in 'yes', 'y'){
	powercfg /batteryreport
	start battery-report.html
} else {
	# Execute pause to prevent the processing results from being lost.
	Write-Host "Press any key to exit..."
	cmd /c Pause | Out-Null
}

スクリプトをbatファイルで起動するようにしてこちらをショートカットに設定している。

batterycheck.bat
powershell -NoProfile -ExecutionPolicy Bypass .\battery_report.ps1

実行結果は問題なく動作していそう。
簡単にバッテリー劣化度合いをチェックできるのでバッテリー交換時期を早めに確認できる。
StartMenu>Programsにショートカットを置いておけばスタートメニューの検索窓でbatterycheckと打ち込み、Enterするだけで簡単チェックができる。

実行結果.
Checking for the battery deterioration level of laptop...
CAPACITY ATTENUATION RATE: 16 %
DESIGN CAPACITY: 45730 mWh
FULL CHARGE CAPACITY: 38390 mWh
Do you want to check the battery report? (yes/no): no
Press any key to exit...
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?