Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. 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

6 Answers 6

I believe setting the environment variable from within powershell with [Environment]::SetEnvironmentVariable as suggested by Dan is pointless because you would either lose the variable's content upon the termination of powershell if you chose the temporary "process" context or would not have it within your batch file's environment yet if you chose the permanent "machine" or "user" context - that is, unless your entire script is written in PowerShell, where the problem would not arise in the first place:

C:\Users\myuser>echo %DOW%
%DOW%

C:\Users\myuser>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

PS C:\Users\myuser> $dow = (get-date).dayofweek
PS C:\Users\myuser> [Environment]::SetEnvironmentVariable("DOW", $dow, "User")
PS C:\Users\myuser> [Environment]::SetEnvironmentVariable("DOW", $dow, "Process")
PS C:\Users\myuser> exit

C:\Users\myuser>echo %DOW%
%DOW%

C:\Users\myuser>

You could use the for command as a workaround to parse the output of your powershell command and put it into a variable

  for /F "usebackq tokens=1" %%i in (`powershell ^(get-date^).dayofweek`) do set DOW=%%i

Note the caret characters ^ used to escape the parenthesis special-chars within your powershell call.

If you are testing it from the command line and not from within a batch file context, you would need to replace the %% before variable references by %:

C:\Users\myuser>for /F "usebackq tokens=1" %i in (`powershell ^(get-date^).dayofw
eek`) do set DOW=%i

C:\Users\myuser>set DOW=Friday

C:\Users\myuser>
share|improve this answer
    
No, this is incorrect. My example below sets 'normal' Environmental variables which are accessible from any batch file etc. –  Dan Jan 13 '12 at 10:19
    
@Dan I believe you are mistaken, I've tried setting [Environment]::SetEnvironmentVariable("DOW", $dow, "user") from within a running powershell session but it was not present in my parent cmd session upon termination of PowerShell –  the-wabbit Jan 13 '12 at 10:22
    
The formatting has screwed that up, but you haven't defined $dow. Running my example in a Powershell script stores a normal environmental variable as expected. –  Dan Jan 13 '12 at 10:26
    
@Dan I have. And obviously the environment variable is stored for the powershell process but it is isolated from the parent CMD process and cannot be accessed there. Just test it and see. –  the-wabbit Jan 13 '12 at 10:28
    
@Dan BTW: I do not mind the downvoting, but it should be noted that the demonstrated approach clearly does what the questioner has asked for, so even if there were a more elegant approach, it still would be a valid solution to the problem. –  the-wabbit Jan 13 '12 at 10:31

You should run both commands in PowerShell as Powershell is more than capable of manipulating Environmental variables.

I.e.:

$dow = (get-date).dayofweek
[Environment]::SetEnvironmentVariable("DOW", $dow, "Machine")

or

[Environment]::SetEnvironmentVariable("DOW", $dow, "User")

By the way, your script doesn't work because all you're getting is the Powershell return code, not the data it produces. There may be a way to get it to work, but it's ultimately pointless compared to just using a proper Powershell script.

For completeness, here is a nice article from MS on Powershell and Environmental Variables:

http://technet.microsoft.com/en-us/library/ff730964.aspx

Update: Having reviewed this solution with @syneticon-dj in chat, it appears the issue you face using this method is that a command prompt has to be reloaded before it will reflect changes in environmental variables that have happened externally.

You haven't provided much detail about what it is you're doing, but if this is the only reason you're launching Powershell, than my actual suggestion would to review how you're doing things.

Either do your whole process using PowerShell or have you considered using scheduled tasks instead? You can schedule tasks based on the day of the week.

share|improve this answer
    
The batch file will be run every day and is run from the scheduled tasks. Im thinking of working in powershell instead. But im even more newbie in powershell than cmd. And right now Im having difficulties with a simple command. But I can make a new question about that one, maybe that's an easy one for you :) EDIT: I've figured it out. It was how to run a program with some parameters. –  Tim Jan 13 '12 at 12:22
    
I'm not great at PowerShell, either, but it's well worth learning. For a start it's just better in almost every way, but it's also the way Microsoft (And so other manufacturers) are moving. –  Dan Jan 13 '12 at 12:24
1  
Also, note that @Dan's suggestion above sets the environment variables quite permanently (either in the Machine or User context). If you leave out the third parameter, you can set it for just the current process, which is sometimes more desirable. –  Per Lundberg Oct 25 '13 at 6:28

you can use something like:

$env:DOW = "foo"
share|improve this answer
1  
I don't understand why was this flagged negatively, do you know of a better to set environment variables? –  Ion Todirel Jun 14 '13 at 8:23
1  
+1 This works great for me in the scenario I needed. PowerShell -Command $env:Note = 'Elevate'; (New-Object -com 'Shell.Application').ShellExecute('cmd.exe', '/k %*', '', 'runas') –  David Ruhmann Jul 11 '13 at 17:23
1  
@IonTodirel Because this only persists in the process space. –  JohnD Jun 19 at 21:24

If it were me, and the parent script has to be a shell script, I'd just do a cheeky invocation of PowerShell in the .CMD script like:

set DOW=
for /f %%D in ('%SystemRoot%\System32\WindowsPowerShell\V1.0\powershell.exe -NoLogo -NoProfile -Command Write-Host -Object ^(Get-Date^).DayOfWeek;') do set DOW=%%D

You might need to check your PowerShell execution policy (Set-ExecutionPolicy cmdlet).

share|improve this answer

Or if you're married to doing this in the old shell, skip Powershell entirely.

Use the %date% variable and expand out the day from the abbreviation it gives (This might be affected by regional date format settings)

C:\>echo %date%
Thu 09/05/2013

Grab the first token in the answer and expand on that:

C:\>type dayofweek.cmd
@echo off
for /f %%A in ("%date%") do set DAYOFWEEK=%%A
if "%DAYOFWEEK%" == "Mon" set DAYOFWEEK=Monday
if "%DAYOFWEEK%" == "Tue" set DAYOFWEEK=Tuesday
if "%DAYOFWEEK%" == "Wed" set DAYOFWEEK=Wednesday
if "%DAYOFWEEK%" == "Thu" set DAYOFWEEK=Thursday
if "%DAYOFWEEK%" == "Fri" set DAYOFWEEK=Friday
if "%DAYOFWEEK%" == "Sat" set DAYOFWEEK=Saturday
if "%DAYOFWEEK%" == "Sun" set DAYOFWEEK=Sunday
echo.%DAYOFWEEK%
C:\>
C:\>dayofweek
Thursday
share|improve this answer

Actually, when putting something like below in a ps1 file (say t.ps1) and calling it from a CMD session with PowerShell -File t.ps1 ... it works well ... $DateAndTime = (get-date -UFormat "%Y%m%d_%H%M%S") [Environment]::SetEnvironmentVariable("DateAndTime", $DateAndTime, "Machine")

But not for the CMD session where the t.ps1 script has been called from. In a new CMD session we get D:\>echo %DateAndTime% ==> 20120208_123106

I guess we have to find out how to get done something like "export T=`date" in a CMD session ...

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.