Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
Friday

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=0
dow = powershell (get-date).dayofweek

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

share|improve this question

3 Answers 3

up vote 4 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%
PAUSE

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.