Windows command prompt: how do I get the output of a command into a environment variable?
I want to have an 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 an environment variable.
C:\Users\tisc> set dow = powershell (get-date).dayofweek
But when I try to get it I don't 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.
You can use something like:
$env:DOW = "foo"
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 Microsoft on PowerShell and environmental variables:
Creating and Modifying Environment Variables
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.
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 characters 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>
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).