How do I give myself Administrator privileges in PowerShell?

John Emmas 181 Reputation points
2026-07-10T14:02:32.8933333+00:00

A program I was experimenting with added this entry to env:PATH in PowerShell:-

C:\Users\JohnE\xmake

Unfortunately, uninstalling the program didn't remove the path entry. I found some PowerShell commands that would supposedly remove it:-

function changePath ($action, $addendum) { $regLocation = "Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" $path = (Get-ItemProperty -Path $regLocation -Name PATH).path # Add an item to PATH if ($action -eq "add") { $path = "$path;$addendum" Set-ItemProperty -Path $regLocation -Name PATH -Value $path } # Remove an item from PATH if ($action -eq "remove") { $path = ($path.Split(';') | Where-Object { $_ -ne "$addendum" }) -join ';' Set-ItemProperty -Path $regLocation -Name PATH -Value $path } }

followed by:-

changePath "remove" "C:\Users\JohnE\xmake"

However, running that command told me I needed Administrator privileges to access the Windows Registry. So I closed PowerShell and re-opened it as Administrator. This time it looked promising - except that env:PATH as an Administrator didn't contain that entry anyway (so nothing got changed). I guess I'll need to restart PowerShell as a normal user but somehow issue the command with Administrator privileges so is that possible somehow?

Windows for home | Windows 10 | Accessibility
0 comments No comments

4 answers

Sort by: Most helpful
  1. John Emmas 181 Reputation points
    2026-07-10T16:03:12.7666667+00:00

    Oh gosh I'm so sorry guys. Given that no path contains C:\Users\JohnE\xmake I figured I'd just try rebooting and that fixed the problem!!!

    Was this answer helpful?


  2. MotoX80 37,861 Reputation points
    2026-07-10T15:40:44.2633333+00:00

    Why don't you just use Settings to manually review and update the environment variables? You will need to check both the user and system variables.

    User's image

    User's image

    Was this answer helpful?


  3. DaveM121 931.8K Reputation points Independent Advisor
    2026-07-10T14:44:58.2933333+00:00

    1

    The easiest method to undo that change is to open the System Restore utility and check if you have a System Restore point dated before that change was made, that will restore the registry and remove the changes made.

    2

    If you do not have a System Restore point, open the Registry Editor.

    Paste this into the address bar an press Enter.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

    In the right hand pane, double click the Path entry

    Carefully look through the ValueDate on that entry dialog, if you find 'C:\Users\JohnE\xmake' in the data, carefully remove it and the semi-colon before it and click OK.

    Then, Restart your PC to apply the change.

    3

    If you were unable to find that in the registry Entry, click your Start Button and just type variables.

    On the resulting list, open the 'Edit Environment Variables' app.

    IN the resulting utility, in the bottom pane, double click the Path variable.

    Are you able to find and delete 'C:\Users\JohnE\xmake' in that variable.

    If so, delete it and restart your PC to apply the change.

    Was this answer helpful?


  4. John Emmas 181 Reputation points
    2026-07-10T14:05:36.6666667+00:00

    Apologies. 'changePath' is supposed to be a function definition I don't understand why all the formatting got removed. Let me try again...

    function changePath ($action, $addendum) {

    $regLocation =

    "Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"

    $path = (Get-ItemProperty -Path $regLocation -Name PATH).path

    # Add an item to PATH

    if ($action -eq "add") {

    $path = "$path;$addendum"

    Set-ItemProperty -Path $regLocation -Name PATH -Value $path

    }

    # Remove an item from PATH

    if ($action -eq "remove") {

    $path = ($path.Split(';') | Where-Object { $_ -ne "$addendum" }) -join ';'

    Set-ItemProperty -Path $regLocation -Name PATH -Value $path

    }

    }

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.