Advertisement

Run as administrator context menu for Power Shell (PS1) files

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.

Advertisement

This can be done with a simple Registry tweak. Follow the instructions below.

  1. Open Registry Editor.
  2. Go to the following Registry key:
    HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell

    ps1-registry-key-1Tip: You can access any desired Registry key with one click.
    If you have no such Registry key, then just create it.

  3. Create here a new subkey named "runas". You will get
    HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\runas

    create-new-run-subkey

    create-new-run-subkey-2

  4. 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:hasluashield
  5. 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

    create-the-command-subkeySet 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'"

    ps1-run-as-administrator-context-menu

Now right click any *.PS1 file to test the context menu item:

ps-run-as-administrator

To save your time, you can download ready-to-use Registry files:

Download 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.

Tweaker Run As Administrator Ps1

You can download Winaero Tweaker here:

Download Winaero Tweaker

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!

Advertisment

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

4 thoughts on “Run as administrator context menu for Power Shell (PS1) files”

  1. 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!)

Leave a Reply

Your email address will not be published.

css.php
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.