Can't add Windows System Variable

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 4 months, 1 week ago.

  • Author
  • #66421
    Profile photo of molo


    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?

  • #66423
    Profile photo of Olaf Soyk
    Olaf Soyk

    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").

    • #66546
      Profile photo of molo


      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?

    • #66561
      Profile photo of Olaf Soyk
      Olaf Soyk

      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 ...

  • #66573
    Profile photo of Max Kozlov
    Max Kozlov

    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

You must be logged in to reply to this topic.