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
  • 13,742
  • 6
  • 28
  • 51
  • 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

1

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
  • 13,656
  • 3
  • 11
  • 30
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
  • 16,942
  • 3
  • 35
  • 62