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"
1
  • FYI, there is no need to use 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. Commented Jul 4, 2020 at 21:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.