2

I have the follow "powershell script"

Get-WMIObject -Query "SELECT * from win32_logicaldisk WHERE DriveType = '3'" | Measure-Object -Property Size -Sum | select -ExpandProperty SUm

And i want to use this to make it happen, but my var variable is empty. I think it's with the " or ' but i'm a newbie and my knowledge isn't that great with scripting. Can someone help me with this silly thing?

FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -noprofile {Get-WMIObject -Query "SELECT * from win32_logicaldisk WHERE DriveType = '3'" | Measure-Object -Property Size -Sum | select -ExpandProperty SUm}`) DO (
SET var=%%F
5
  • You need to escape the ^|. Commented Mar 3, 2021 at 16:27
  • @Squashman - That's what I thought too, but there's apparently more than that Commented Mar 3, 2021 at 16:30
  • 1
    If you're new to scripting, I'd say that embedding a Powershell pipeline into a CMD batch file. Why not do it all in Powershell? At which point (Powershell 7 here), you just have $size = (Get-CimInstance -Query "SELECT * from win32_logicaldisk WHERE DriveType = '3'" | Measure-Object -Property Size -Sum).Sum and then you can just refer to that elsewhere in the Powershell script? Commented Mar 3, 2021 at 17:45
  • Agree with previous commenter - why batch at all? Why not just use PowerShell for the whole script? Commented Mar 3, 2021 at 22:52
  • You can take a look too at Windows Command line get disk space in GB Commented Mar 4, 2021 at 14:17

2 Answers 2

3

The main problem is that a lot of characters in PowerShell do other things in batch, and they interfere with how for loops work. Combine that with the extra of layer of string processing that goes into for /F loops, and there's a bunch of extra escaping that is needed.

Ultimately, quotes around everything and double-quoting plus escaping inner quotes seems to do the trick.

@echo off
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell -noprofile "Get-WMIObject -Query \""SELECT * from win32_logicaldisk WHERE DriveType = '3'\"" | Measure-Object -Property Size -Sum | select -ExpandProperty Sum"`) DO (
    SET var=%%F
)
echo %var%
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a bunch! never got it right with the quotes or double quotes.
Honestly, I just tried a bunch of stuff until it worked.
is it possible to round the outcome (%var%)
Round it to what? I assume powershell has some sort of rounding function, but everything I see is about rounding values with decimals and it's a little weird that your sum of all hard drive sizes isn't an integer.
Thanks, @Aacini. PowerShell is weird about when it needs extra stuff in order to work correctly. I wish Microsoft hadn't crippled it by default so it would just work like a normal scripting language.
2

I prefer using another way. I think it's simplier to understand and I hate cmd's for syntax

:createTempFileLoop
set "MY_TEMPFILE=%TEMP%\my~%RANDOM%~%RANDOM%~%RANDOM%~%RANDOM%~%RANDOM%.cmd"
if exist "%MY_TEMPFILE%" goto :createTempFileLoop

SET MY_SUMSIZE=-1

@REM Variant powershell.exe -Command [IO.File]::WriteAllText('%MY_TEMPFILE%', 'SET MY_SUMSIZE='+(Get-WMIObject -Query 'SELECT * from win32_logicaldisk WHERE DriveType = ''3''' ^| Measure-Object -Property Size -Sum).SUM, [System.Text.Encoding]::ASCII)

powershell.exe -Command 'SET MY_SUMSIZE='+(Get-WMIObject -Query 'SELECT * from win32_logicaldisk WHERE DriveType = ''3''' ^| Measure-Object -Property Size -Sum).SUM > %MY_TEMPFILE%

call %MY_TEMPFILE%
DEL /Q /F %MY_TEMPFILE%
echo Size is %MY_SUMSIZE%
pause

1 Comment

Thanks for your solution, it does do the same as the solution form @SomethingDark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.