- This topic has 4 replies, 3 voices, and was last updated 3 years, 7 months ago by
March 15, 2017 at 10:07 am #66421ParticipantTopics: 2Replies: 1Points: 1Rank: Member
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 #66423ParticipantTopics: 7Replies: 2458Points: 6,439Rank: Community MVP
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 #66546ParticipantTopics: 2Replies: 1Points: 1Rank: Member
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 #66561ParticipantTopics: 7Replies: 2458Points: 6,439Rank: Community MVP
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 #66573ParticipantTopics: 2Replies: 376Points: 1Rank: Member
you can run powershell directly from scheduler, and tick ‘execute with highghest privileges’
you can even runPowerShell1powershell.exe -ExecutionPolicy Bypass -Command "[System.Environment]::SetEnvironmentVariable('TestVariable02', 'Test value 02', 'Machine')"
if you need to run only this command
- The topic ‘Can't add Windows System Variable’ is closed to new replies.