How to Capture Command Output to Environment Variable in Windows CMD: PowerShell Get-Date Example


11 views

When working with Windows Command Prompt (cmd.exe), you might encounter issues when trying to store command output directly into environment variables. The naive approach using set var = command doesn't work as expected because:

REM This doesn't work as intended
set dow = powershell (get-date).dayofweek

The above actually creates a variable literally containing the command text rather than executing it and storing the output.

Here are three reliable methods to capture command output into environment variables:

Method 1: Using FOR /F with PowerShell

for /f "delims=" %%a in ('powershell (get-date).dayofweek') do set DOW=%%a
echo %DOW%

Method 2: Native CMD without PowerShell

for /f "tokens=1" %%a in ('wmic path win32_localtime get dayofweek /value ^| find "="') do set DOWNUM=%%a
set DOW=
if %DOWNUM% equ 0 set DOW=Sunday
if %DOWNUM% equ 1 set DOW=Monday
REM ...continue for all days
echo %DOW%

Method 3: Using Temporary Files

powershell (get-date).dayofweek > %temp%\dow.txt
set /p DOW=<%temp%\dow.txt
del %temp%\dow.txt
echo %DOW%

Here's how you might implement this in a backup batch file:

@echo off
:: Get current day of week
for /f "delims=" %%a in ('powershell (get-date).dayofweek') do set DOW=%%a

:: Create backup directory with day name
mkdir "C:\Backups\%DOW%"

:: Run your backup commands
xcopy "C:\ImportantData\*" "C:\Backups\%DOW%\" /s /e /h /k /y

echo Backup completed for %DOW%

For PowerShell 5.1+, you can use this more concise syntax:

for /f "delims=" %%a in ('powershell -command "[DateTime]::Now.DayOfWeek"') do set DOW=%%a

When working with Windows command-line automation, a common requirement is storing command output in environment variables for later use. The naive approach of set var = command doesn't work as expected because CMD processes the assignment literally rather than executing the command first.

The most reliable method in pure CMD uses the FOR /F construct to capture output:

@echo off
FOR /F "delims=" %%G IN ('powershell -command "(get-date).DayOfWeek"') DO SET DOW=%%G
echo Current day: %DOW%

Key points about this solution:

  • The "delims=" prevents splitting the output at spaces
  • Works with both standard CMD commands and PowerShell calls
  • Variables persist for the current CMD session

If you're already in a PowerShell context, the approach is simpler:

$dow = (Get-Date).DayOfWeek
[Environment]::SetEnvironmentVariable("DOW", $dow, "User")

Here's how this technique applies to your backup scenario:

@echo off
:: Capture day of week
FOR /F "delims=" %%G IN ('powershell -command "(get-date).DayOfWeek"') DO SET DOW=%%G

:: Create backup directory with timestamp
FOR /F "delims=" %%H IN ('powershell -command "Get-Date -Format 'yyyyMMdd_HHmm'"') DO SET TIMESTAMP=%%H

mkdir "C:\Backups\%DOW%_%TIMESTAMP%"
xcopy "C:\Data\*.*" "C:\Backups\%DOW%_%TIMESTAMP%\" /E /H /C /I
  • Always test with echo %VAR% after assignment
  • For multi-line output, add usebackq option to FOR command
  • Trim whitespace with SET VAR=%VAR: =% if needed

To make variables persist beyond the current session:

:: Temporary (session-only)
SET DOW=Friday

:: Permanent (requires admin)
SETX DOW "Friday"