There are not many cases when you need to execute a Power Shell script (*.PS1 file) as Administrator. But when you need it, it can be tricky. Instead of just double clicking your PS1 file, you need to open an elevated Power Shell console and type the name of the PS1 script file at the correct path to execute the script as admin. To avoid this, you can add a context menu command for PS1 files to the File Explorer app which will allow you to run the selected PS1 file as administrator. Here is how.
Advertisеment
This can be done with a simple Registry tweak. Follow the instructions below.
- Open Registry Editor.
- Go to the following Registry key:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell
Tip: You can access any desired Registry key with one click.
If you have no such Registry key, then just create it. - Create here a new subkey named "runas". You will get
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\runas
- Under the runas subkey, create a new string value named HasLUAShield. Don't set its value data, leave it empty. This value is only required to add the UAC icon to the context menu item you are creating. You should get it as follows:
- Under the runas subkey, create a new subkey called "command". You will get the following path:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\runas\command
Set the default parameter of the command subkey to the following text:
powershell.exe "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Now right click any *.PS1 file to test the context menu item:
To save your time, you can download ready-to-use Registry files:
Use them to add or remove the mentioned context menu entry.
Alternatively, you can use Winaero Tweaker and enable or disable the feature with one click.
You can download Winaero Tweaker here:
You are done. To undo this change, delete the mentioned "runas" subkey.
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!
Advertisеment
Thank you for this!
You are welcome.
Umm, yeah this is awesome! Thanks!!!
An alternative to using the “runas” key is having a regular “Command” key and using this value for its “(Default)”: powershell.exe -command ” Start-Process PowerShell.exe -Verb RunAs -ArgumentList \””-Command `\””cd ‘%W’; %1;`\””\””; ”
Yeah, it’s more complicated. Just including it here because I didn’t know about the “runas” key before I tried implementing a “Run script as admin” command, so figured I’d at least mention the alternative I put together. (which took quite some time due to the terrible command-line handling of quotes!)