Can't add Windows System Variable

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

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #66421

    Participant
    Points: 0
    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
    Points: 749
    Helping Hand
    Rank: Major Contributor

    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
      Points: 0
      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
      Points: 749
      Helping Hand
      Rank: Major Contributor

      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
    Points: 0
    Rank: Member

    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

The topic ‘Can't add Windows System Variable’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort