Run PowerShell script as Administrator

From DanIT
Revision as of 21:06, 29 June 2022 by Dan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

About

Right-click on a PowerShell script

By default on Windows, there is no easy way to run a PowerShell script as administrator.
You cannot elevate from the script with 'Run with PowerShell', so you have to run PowerShell from Start as administrator, browse to the directory of the script, and open/run it every time.
This small add-on will give you the ability to "Run as Administrator" any PowerShell .ps1 script file, from the right-click menu.

Install

Manual

  1. Open Registry Editor (regedit.exe).
  2. Browse to: HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell
  3. Create a new Key called: RunAs
  4. In this Key, create a new Key called: Command
  5. In this Key, edit the (Default) Key
  6. Enter the same value as the (Default) value in "0\Command", in this case:
    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

Automatic

This file will also add icons to all the PowerShell menu options.

Download .reg-file: File:PowerShell Run As Administrator.reg

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\0]
"Icon"="%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit]
"Icon"="%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell_ise.exe"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\RunAs]
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\RunAs\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""