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:
You should run both commands in PowerShell as PowerShell is more than capable of manipulating environmental variables.
I.e.:
or
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:You could use the
for
command as a workaround to parse the output of your PowerShell command and put it into a variable: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%
: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:
You might need to check your PowerShell execution policy (Set-ExecutionPolicy cmdlet).
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):
Grab the first token in the answer and expand on that:
Actually, when putting something like the below in a .ps1 file (say,
t.ps1
) and calling it from a CMD session with PowerShell-File t.ps1
... it works well.But not for the CMD session where the t.ps1 script has been called from. In a new CMD session we get:
I guess we have to find out how to get done something like
export T=date
in a CMD session.If you want that your batch file to access environment variables that are set by a PowerShell command run from that batch file, you can use the following workaround:
Make your PowerShell script create a sub batch file,
mysub.bat
, that contains "set variable=value" lines, and execute that mysub.bat batch file from the main batch file right after the PowerShell command.Use the WriteAllLines method rather than the default PowerShell output methods so that the sub batch file be not generated with the UTF-8 encoding format.
Example
main.bat: