* You are viewing the archive for September, 2018

Use PowerShell utilities in your Windows CMD scripts

Want a random number in your Windows command file? This little trick works wonders and uses PowerShell to generate the number, mapping the returned value to a script variable.

for /f “usebackq” %%i in (`PowerShell $randomNumber ^= Get-Random^; $randomNumber;^`) do set VALUE=%%i
echo Value is %VALUE%

Use the same pattern to enrich your CMD scripts with other PowerShell utilities and code, for example…

for /f “usebackq” %%i in (`PowerShell $date ^= Get-Date^; $date.ToString^(‘yyyy-MM-ddThh:mm:ss’^)`) do set LOGDATE=%%i
echo Date is %LOGDATE%

just remember to escape any script special characters (< > | etc) using a caret (^)