15

I'm trying to schedule a command in a PowerShell script to run using Windows scheduler. This command relies on a user specific environment variable, and since scheduled tasks do not have access to these I have decided to try and just set the variable inside the PowerShell script itself.

The normal environment variable configuration looks like this:

Enter image description here

And in order to replicate this in the script, I tried using:

$env:DCRCLIFILE = "C:\Users\pzsr7z\Desktop\DCRCLIFILE.txt"

But for some reason the program will not work properly when I set things up this way, even though it works perfectly fine when I run the program using the "normal" user environment variable.

Program execution using the PowerShell initiated environment variable

Image description

Program execution using the "normal" environment variable:

Enter image description here

Is there anything that I should be doing differently when setting the environment variable in the PowerShell script? I'm not sure why this isn't working. I've verified that the environment variable actually gets created, so it's not like the PowerShell command is failing.

11
  • "since scheduled tasks do not have access to these" - whatever gave you that impression? Commented Nov 18, 2016 at 16:04
  • @Bill_Stewart If you set a scheduled task that executes "Get-ChildItem Env:", you will notice in the output that all of the system environment variables are shown while the user specific environment variables are left out. At least that's how it works on the windows 2012 server that I'm running this on Commented Nov 18, 2016 at 16:07
  • Under which account is the variable defined, and under which account are you executing the task? Commented Nov 18, 2016 at 16:25
  • @Bill_Stewart When I created the variable I just logged in as my normal user and then went to control panel, system, advanced system settings, and set the variable in the "User Variables for pzsr7z" section. For the scheduled task, I just created the task and left the user as the default one which is gxx\pzsr7z. So the same user is both creating and utilizing the variable. In case it makes any difference, the scheduled task is running when I'm logged out of the server (once I get this working it will execute once a day) Commented Nov 18, 2016 at 16:36
  • I just created a PowerShell script that outputs the content of a user environment variable to a file, and scheduled it to run. The variable was accessible when I used the "run whether user is logged on or not" option. I suspect something else is at play here, as user environment variables should be accessible in scheduled tasks (no reason they shouldn't be). Commented Nov 18, 2016 at 16:43

2 Answers 2

33
$env:DCRCLIFILE = "C:\Users\pzsr7z\Desktop\DCRCLIFILE.txt"

This only creates a process-level environment variable, which is only visible to, and lasts only as long as, your current PowerShell session.

You need to set a more permanent environment variables (user-level or machine-level) by using the .NET Framework and the SetEnvironmentVariable method:

[Environment]::SetEnvironmentVariable("DCRCLIFILE", "C:\Users\pzsr7z\Desktop\DCRCLIFILE.txt", "User")
Sign up to request clarification or add additional context in comments.

6 Comments

Would that take effect immediately though? When I ran that command I had to logout and then log back in before variable became available
Environment variables are normally only read when a process starts, if you make a change to them you need to restart the process before it can see the changes.
so what would be the best way to get this working when running things using windows scheduler? I would need to restart powershell after creating the environment variable, but wouldn't I just not be able to access the new variable after the restart since scheduled tasks do not have access to user created environment variables?
Have you tried setting the Env Var as a Machine Variable?
I would prefer to avoid that if possible as other people use this server as well, so having this variable globally available would interfere with things when they attempt to run the dcrcli.exe command that was shown in the screenshots in my post
|
0

I ended up solving this issue by creating a separate batch file and then setting the DCRCLIFILE variable manually in there, after which I ran the dcrclifile.exe command from within the same batch file.

I'm not sure why things did not work properly when I was attempting to set the variable manually in PowerShell.

1 Comment

I don't know either. It works fine for me in PowerShell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.