你可以使用PowerShell在Windows中通过应用程序获取电池使用情况。

huangapple go评论65阅读模式
英文:

How can you get battery usage by app in Windows using PowerShell?

问题

你可以使用powercfg /batteryreport来获取电池使用情况,但这不像下面的图中的“电池使用情况”那样显示应用程序的电池使用情况。

如何使用PowerShell获取应用程序的电池使用情况?

英文:

You can get battery usage using powercfg /batteryreport, but this doesn't show battery usage by app like in Battery Usage in the following image.

How can you get battery usage by app using PowerShell?

你可以使用PowerShell在Windows中通过应用程序获取电池使用情况。

答案1

得分: 2

不,你不能使用PowerShell来做这件事。

英文:

No, you can’t do this using PowerShell.

答案2

得分: 1

获取Windows中应用程序的电池使用情况,可以使用PowerShell,您可以使用以下脚本检索每个运行中应用程序的电池使用信息,并按电池消耗降序显示它:

# 获取应用程序的电池使用情况

# 获取运行进程的列表
$processes = Get-WmiObject Win32_Process -Filter "name <> 'Idle'" | Select-Object ProcessID, Name

# 初始化一个哈希表以存储每个应用程序的电池使用情况
$batteryUsage = @{}

# 遍历每个运行中的进程
foreach ($process in $processes) {
    $processID = $process.ProcessID
    $processName = $process.Name

    # 获取进程的电池使用信息
    $powerInformation = Get-WmiObject -Namespace root\cimv2\power -Class Win32_ProcessPowerUsage | Where-Object { $_.ProcessID -eq $processID }

    # 如果存在电池使用信息
    if ($powerInformation) {
        $batteryUsage[$processName] = $powerInformation.ElapsedTime
    }
}

# 按电池使用情况降序对哈希表进行排序
$sortedBatteryUsage = $batteryUsage.GetEnumerator() | Sort-Object -Property Value -Descending

# 显示应用程序的电池使用情况
foreach ($entry in $sortedBatteryUsage) {
    $appName = $entry.Name
    $batteryTime = $entry.Value / 10000000  # 将经过的时间转换为秒
    Write-Host "$appName: $batteryTime 秒"
}

此脚本使用WMI检索电池使用信息。

英文:

To get Battery usage by application in Windows using powershell, you can use the script which retrieves battery usage information for each running application and displays it in descending order of battery consumption.

# get the battery usage by app

# get the list of running processes
$processes = Get-WmiObject Win32_Process -Filter &quot;name &lt;&gt; &#39;Idle&#39;&quot; | Select-Object ProcessID, Name

#  initialize a hashtable to store the battery usage for each app
$batteryUsage = @{}

#loop through  each running process
foreach ($process in $processes) {
    $processID = $process.ProcessID
    $processName = $process.Name

    # Get the battery usage information for the process
    $powerInformation = Get-WmiObject -Namespace root\cimv2\power -Class Win32_ProcessPowerUsage | Where-Object { $_.ProcessID -eq $processID }

    # If battery usage information is available
    if ($powerInformation) {
        $batteryUsage[$processName] = $powerInformation.ElapsedTime
    }
}

#sort the hashtable by battery usage in descending order
$sortedBatteryUsage = $batteryUsage.GetEnumerator() | Sort-Object -Property Value -Descending

# Display the battery usage by app
foreach ($entry in $sortedBatteryUsage) {
    $appName = $entry.Name
    $batteryTime = $entry.Value / 10000000  # Convert the elapsed time to seconds
    Write-Host &quot;$appName: $batteryTime seconds&quot;
}

this script retrieves battery usage information using WMI.

huangapple
  • 本文由 发表于 2023年6月6日 11:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定