1

I created a batch file that executes a PowerShell command. This should read out the capacity of the notebook battery. How can the result of the command be defined as a variable?

Something with tokens is needed for that, right? Can someone help me? In the end, CMDs/Natchs "echo" should output the result.

powershell -Executionpolicy Bypass -Command "(Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity"
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • FYI, there is no need to use [tag:powershell] for this task. Although regardless of which method you use to retrieve the WMI information, the linked duplicate question should provide the appropriate methodology. – Compo Jul 04 '20 at 21:59

2 Answers2

2

Try this:

for /f "tokens=* delims= " %%a in ('powershell -Executionpolicy Bypass -Command "(Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity"') do set "var=%%a"

You can replace var with variable name.

Wasif
  • 14,755
  • 3
  • 14
  • 34
1

You can try like this batch code using for in do loop : for /f

@echo off
Set PS_CMD=Powershell ^
"(Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI"^).FullChargedCapacity"

@for /f "tokens=* delims=" %%a in ('%PS_CMD%') do set "var=%%a"
    echo %Var%
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70