Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to have a environment variable that contains the day of week in cmd.exe.

When I run this command I get the result I want.

C:\Users\tisc>powershell (get-date).dayofweek

Here I'm trying to store the result in a environment variable.

C:\Users\tisc>set dow = powershell (get-date).dayofweek

But when I try to get it I dont get the string as I wanted.

C:\Users\tisc>set dow
dow = powershell (get-date).dayofweek

My goal is to use the variable in a batch file for some backup scripts.

share|improve this question
up vote 3 down vote accepted

To do it without a temp file you can do:

@ECHO off
FOR /F %%i in ('powershell.exe -noprofile "(get-date).DayOfWeek"') DO SET dow=%%i
ECHO Day of week %dow%

from the command prompt remove the double %%:

FOR /F %i in ('powershell.exe -noprofile "(get-date).DayOfWeek"') DO SET dow=%i

Using -noprofile will make it run faster if you have a profile set up for PowerShell.

share|improve this answer
powershell -noprofile -command {[environment]::SetEnvironmentVariable("dow", (get-date).DayofWeek, "User")}

Obviously you need to open other cmd to see the change.

share|improve this answer

I don't believe you can't do it directly, but you can use a temporary file to hold the result, and import it back into the variable:

@echo off
powershell (get-date).dayofweek > myTmpFile
set /p dow= < myTmpFile
del /q myTmpFile
echo %dow%
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .