How to Run PowerShell Script as Administrator?

When you open the PowerShell console on Windows via the Search bar, shortcut, or pinned taskbar item, the powershell.exe process starts in unprivileged mode. In this mode, you won’t be able to make system changes or perform administrative tasks. In this article, we’ll walk through a few easy ways to run a PowerShell console or script as an administrator.

Run PowerShell as Administrator Using Windows Search Bar

  1. Click on the Start button or press the Windows key on your keyboard;
  2. Type powershell in the search field;
  3. Right-click on the Windows PowerShell icon and select Run as administrator (or select this item in the right pane);
    run powershell script as administrator
  4. A UAC window will appear, in which you need to confirm the start of the process in the elevated mode.

There is also a separate item for starting PowerShell with elevated permissions in the Win+X menu of Windows 10 and 11. Just press Windows + X on your keyboard or right-click the Start button and select Windows PowerShell (Admin).

how to run ps1 file in powershell as administrator

Create Taskbar Shortcut to Run PowerShell as Administrator

If you often run the PowerShell console as an administrator, you can create a shortcut to start the PowerShell in elevated mode automatically.

ADVERTISEMENT
  1. Find the PowerShell icon in the start menu, right-click on it and select Pin to taskbar;
  2. Right-click on the PowerShell icon in the taskbar, select Windows PowerShell > right-click > Properties;
    run ps1 as admin
  3. Click the Advanced button and enable the Run as administrator option;
  4. Click OK twice.
    how to run powershell script as admin

Now, when you click on the PowerShell icon in the taskbar, the PS console will always open in privileged mode.

How to Run a PowerShell Script as Administrator?

When you run any PowerShell script (PS1 file) in an elevated session, it runs as an administrator. Or you can run a PowerShell script file as an administrator with the following command:

Start-Process powershell -verb runas -ArgumentList "-file C:\PS\Scripts\myPSScript.ps1"

Hint. Make sure the PowerShell Execution Policy on your computer is not blocking PowerShell scripts from running.

You can open the console as an administrator with the following command:

powershell Start-Process powershell -Verb runAs

You can execute this command in the cmd.exe command prompt, PowerShell, or using the Run feature.

run powershell script as admin

When you run this command, a UAC window appears asking you to confirm privilege escalation:

User Account Control
Do you want to allow this app to make changes to your device?
Windows PowerShell

powershell script run as administrator

Running PowerShell as Administrator without UAC Prompt?

It is not recommended to disable UAC on Windows. Therefore, you can implement one of the following scenarios to run a PowerShell console with administrator rights without a UAC prompt. It is assumed that your account is a member of the local Administrators group. Otherwise, Windows will ask you to enter the Administrator password on a UAC prompt.

You can:

  • Run powershell.exe from Task Manager.
  • Create a new scheduled task with the checkbox “Run with highest privileges” enabled.

In the first case:

  1. ) Open the Task Manager (Ctlr+Alt+Esc or right-click on the taskbar and select Task Manager);
  2. Select the menu item File > Run new task;
    how to run powershell script as administrator
  3. In the Open window, type powershell and enable the option Create the task with administrative privileges;
    powershell run script as administrator
  4. Click OK. The PowerShell console should open without a UAC prompt.

Now let’s create a scheduled task to run PowerShell as administrator:

ADVERTISEMENT
  1. Open the Task Scheduler console (taskschd.msc);
  2. Select Action > Create Task;
  3. Specify the task name PowerShellAdminTask;
  4. Check the option Run with highest privileges;
  5. Select Windows 10 in the Configure form;
  6. Uncheck the Hidden checkbox; how to run a powershell script as admin
  7. Go to the Actions tab and select New > Start a program;
  8. Copy the following line to the Program field C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe;
    execute powershell script as administrator
  9. Save your task.

Now, to run the PowerShell console as an administrator without a UAC prompt, select the PowerShellAdminTask task in the Task Scheduler console, right-click on it, and select Run.

run ps1 as administrator

Or you can run the job from a non-elevated command prompt with the command:

schtasks.exe /run /tn PowerShellAdminTask

ps1 run as administrator

You can use the Task Scheduler solution to run PowerShell scripts as an administrator without the user’s input. In this case, in the Add argument field, you need to specify the path to your PowerShell script in the following format:

-executionpolicy bypass -file c:\ps\scripts\myposhscript.ps1

how to run a ps1 file as administrator

In this case, the script will be run as an administrator, even if the PowerShell Execution policy is enabled, which blocks unsigned scripts from running.

ADVERTISEMENT

Check if PowerShell is Running as an Administrator

If your powershell.exe console is running with administrator privileges, the window title will say Administrator: Windows PowerShell.

run ps1 file as administrator

In some PowerShell scripts, verifying if the script is running with administrator privileges is sometimes necessary before starting any action.

Run the command:

[Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544'

If the command returned True, then this PowerShell session was started with administrator permissions.

run script as administrator

In PowerShell v4.0 and newer, you can use a special statement at the beginning of your scripts to prevent a PowerShell script from running as a regular (non-administrator) user:

#Requires -RunAsAdministrator

If a script with such a statement is executed in a non-elevated PowerShell process, you will receive an error message:

ADVERTISEMENT

The script ‘test_admin_script.ps1’ cannot be run because it contains a “#requires” statement for running as an Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again.

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.

5 comments

  1. which leaves us with the only both uncovered and the only practical application:
    how to actually execute a powershell script in elevated mode without user’s input.
    cause the idea behind writing a script is automation and the idea behind automation is that user’s input is not required.

  2. It works just fine in my pipelines. Make sure that the Powershell is in the taskbar and properties are Advanced->Run as admin

    In your runs put the line he referred to run: |
    #Requires -RunAsAdministrator
    Start-Process -Wait -FilePath ..

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.