Some commands or tasks require elevation, meaning they must run in the context of an administrator. Such is true with PowerShell scripts, too. Running PowerShell scripts as an administrator means the PowerShell session must be elevated.
There are several ways to run PowerShell scripts as an administrator interactively and non-interactively—which is suitable for automation. We’ll cover them in this article, so stick around and enjoy.
Table of Contents
Ways to Run PowerShell as Administrator (Interactive)
Running PowerShell scripts as an administrator requires an elevated PowerShell session. In most cases, doing so interactively is sufficient for users to run one-off or seldom-used scripts.
Here are the ways to interactively start PowerShell as an administrator so you can run your scripts and commands in God mode.
Note. These methods will trigger the UAC prompt to appear.
Windows Power User Menu
The Power User Menu is the quickest way to start PowerShell as an administrator.
Press Win+X on the keyboard or right-click the Start button to pull up the Power User Menu and click Windows PowerShell (Admin).
Note that on a Windows 11 computer, the Windows PowerShell (Admin) is replaced with Terminal (Admin) by default.
Run as Administrator Context Menu
The context menu (right-click pop-up menu) of executable files and their shortcuts include the Run as administrator context menu. This means you can right-click and select Run as administrator whenever you see the Windows PowerShell or PowerShell icon. Here are some examples:
- From the Start Menu.
- From the Taskbar:
- From the Windows Tools on Windows 11:
- From the Windows Search:
Create a PowerShell Shortcut with Run as Administrator Enabled
You can also create a shortcut to run PowerShell as administrator.
- Right-click anywhere on the desktop and click New → Shortcut.
- Type powershell.exe (Windows PowerShell) or pwsh.exe (PowerShell 7+) and click Next.
- Type the shortcut name and click Finish.
- Right-click the new shortcut and click Properties.
- Click Advanced on the Properties window. Check the “Run as administrator” box and click OK → OK.
Every time you launch PowerShell using the new shortcut, it will run as administrator.
Start PowerShell as Administrator from CMD
Another way to run PowerShell as administrator is by invoking it from the command line or as a CMD script.
From the CMD prompt, run one of the following commands to run Windows PowerShell or PowerShell Core:
REM Run Windows PowerShell as Admin
PowerShell -Command "& {Start-Process powershell -Verb RunAs}"
REM Run PowerShell Core as Admin
Pwsh -Command "& {Start-Process Pwsh -Verb RunAs}"
You can also create a CMD script using these commands. Open Notepad, paste one of the previous commands, and save it as a CMD file.
You can then run PowerShell as administrator by double-clicking on the CMD file.
Start a PowerShell Instance in Task Manager
The Task Manager is a high-privilege process that requires elevation. This means that when opening the Task Manager, the UAC prompt appears.
But once you’re in Task Manager, you can run PowerShell as administrator from it, and it will no longer trigger the UAC prompt.
- On the Task Manager window, click Run new task.
- Enter powershell.exe for Windows PowerShell or pwsh.exe for PowerShell Core.
- Check the “Create this task with administrative privileges” box and click OK.
Ways to Run PowerShell as Administrator (Non-Interactive)
What if you must run a PowerShell script unattended with administrative privileges? Like, in an automation scenario, when a script must run without user intervention and the UAC prompt must be bypassed?
In this example, I’m using a script called “RunMeAsAdmin.ps1” with the following code. This script outputs a message indicating whether it is run as administrator.
if (([System.Security.Principal.WindowsPrincipal] [System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)){
"Run as admin - SUCCESSFUL"
}
else {
"Run as admin - FAILED"
}
Start-Sleep -Seconds 10
As a Task with the Highest Privileges Enabled in Task Scheduler,
The out-of-the-box solution to run PowerShell scripts with non-interactive administrative rights is adding a Task Scheduler entry. The scheduled task can be configured to run with the highest privileges without triggering the UAC prompt.
Related post. Run PowerShell Script with Task Scheduler.
- Open the Task Scheduler and click “Create a task.”
- Under the General tab, configure the following:
- The name is RunMeAsAdmin.
- Select Run only when user is logged on.
- Check Run with highest privileges.
- Configure for your operating system version (eg. Windows 10 or Windows Server 2022).
- Switch to the Actions tab and click New.
- In the New Action window:
- Type powershell.exe for Windows PowerShell or pwsh.exe for PowerShell Core in the “Program/script” box.
- Type -file “path_to_script” inside the “Add arguments” box where path_to_script is the full path of your script.
- Click OK to close the New Action window.
Optionally, you can add a scheduled trigger if needed. But we’ll skip this step for this example.
- Click OK to save the new task.
- Once the task is created, select it from the list and click Run in the Actions pane.
- Or you can trigger it from PowerShell by running the below command:
Start-ScheduledTask -TaskName <task name>
As you can see below, the task is triggered in a non-administrator PowerShell session, and the script runs in an Administrator session.
Related. List, Add, Edit, and Delete Scheduled Tasks with PowerShell.
Run PowerShell Scripts in an Automation Server (CI/CD)
A better solution to run PowerShell scripts with administrator privileges is through automation servers or CI/CD solutions. One example is Jenkins with the PowerShell plugin.
This article does not cover the Jenkins installation and configuration. For more information, refer to How to Install Jenkins with Let’s Encrypt SSL Certificate on Windows.
Suppose you have Jenkins. You can create a new Freestyle project. In this example, I’ll name it Run Me As Admin.
Add a PowerShell step under Build Steps, paste the script, and click Save.
Once the project is created, you will see it on the dashboard. Click the Build Now button to trigger the build (run the PowerShell script.)
After the build, click the build number under the Build History and select Console Output.
Review the console output; you should see a similar result to the one below.
Conclusion
Running PowerShell scripts as an administrator is optional, but knowing it is possible when necessary is good. The methods we explored in this tutorial produced the same results but exhibited different behaviors or user experiences, especially in systems where UAC is turned on.
Solutions to running PowerShell scripts as an administrator that bypasses the UAC prompt are also available. For automation purposes or without user intervention, you can use the built-in Task Scheduler to run PowerShell script with elevated privileges.
Third-party automation servers are also excellent for running PowerShell scripts as an administrator, such as Jenkins, which we briefly demonstrated.
5 comments
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.
I wish you find out and are willing to share the answer you got….because I am in the same boat
I am exactly facing this issue, I cant find a way to prompt the user to concede administrator privileges
How to Run a PowerShell Script as Administrator?
This paragraph not working. Why you write not tested script? wtf.
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 ..