Can't add Windows System Variable

Welcome Forums General PowerShell Q&A Can't add Windows System Variable

Viewing 2 reply threads
  • Author
    Posts
    • #66421
      Participant
      Topics: 2
      Replies: 1
      Points: 1
      Rank: Member

      Hello,

      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”

      Error:
      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
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: 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”).

      • #66546
        Participant
        Topics: 2
        Replies: 1
        Points: 1
        Rank: Member

        Thanks.

        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
        Participant
        Topics: 7
        Replies: 2458
        Points: 6,439
        Helping Hand
        Rank: 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 …

    • #66573
      Participant
      Topics: 2
      Replies: 376
      Points: 1
      Rank: Member

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

      you can even run

      if you need to run only this command

Viewing 2 reply threads
  • The topic ‘Can't add Windows System Variable’ is closed to new replies.