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.

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-1 Tip: 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-subkey 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'"

    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.

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

  1. David H Johnson

    Thank you for this!

    Reply
    1. Sergey Tkachenko Post author

      You are welcome.

      Reply
  2. G-Style

    Umm, yeah this is awesome! Thanks!!!

    Reply
  3. Stephen Wicklund

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *