Author Posts

March 15, 2017 at 10:07 am


I got error when I tried to use Windows Batch file and Powershell PS1 file to add a System Environment Variable:

Powershell PS1 file:
[System.Environment]::SetEnvironmentVariable("TestVariable02", "Test value 02", "Machine");

Windows Batch file:
powershell.exe -ExecutionPolicy Bypass -Command "C:\Details\Powershell\test.ps1"

Exception calling “SetEnvironmentVariable” with “3” argument(s): “Requested registry access is not allowed.”

My Windows login account has already been added to the local Administrator Group.
But still got the above error.
However, if I used local Administrator to login and then ran the BAT file, it worked fine. No error.

How can I fix the error? Where is the cause of the error?

March 15, 2017 at 10:21 am

You have start the cmd "as administrator" even if your account is already local Administrator. But why do you use a batch file to run a powershell script? You can run the Powershell script directly (but anyway you have to run it "as administrator").

March 16, 2017 at 3:02 am


I ran it as a batch file as I wanted to run it in schedule.
You suggested 'Run as administrator', but how can we do it if we run a powershell script in schedule?

March 16, 2017 at 8:44 am

I don't have an english client at the moment to look it up but it should be on the first tab when you create a new scheduled task and it should be something like execute or run with highest privileges.

... found this screenshot ...

Schedlued Tasks ... it's almost on the bottom ...

March 16, 2017 at 10:12 am

you can run powershell directly from scheduler, and tick 'execute with highghest privileges'

you can even run

powershell.exe -ExecutionPolicy Bypass -Command "[System.Environment]::SetEnvironmentVariable('TestVariable02', 'Test value 02', 'Machine')"

if you need to run only this command