How to set a environment variable from a powershell command

command linepowershell

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.

Best Answer

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.