Loading...
X

PowerShell with administrator rights: how to run and verify

How to run PowerShell as an administrator

Some commands and cmdlets require elevated privileges. Examples of actions that require administrator rights are service management (install, start, stop, uninstall) and package management (add and remove package providers, install and remove packages).

For example, the following cmdlet tries to add a new package provider:

Install-PackageProvider chocolatey

It will throw the following error:

Install-PackageProvider: Administrator rights are required to install packages in ''. Log on to the computer with an account that has Administrator rights, and then try again, or install in 'C:\Users\MiAl\AppData\Local\PackageManagement\ProviderAssemblies' by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).

To fix this error, you need to run PowerShell as an administrator.

How to run PowerShell as an administrator on Windows

Let's consider several ways.

1. Launch PowerShell with administrator rights via the Power user menu

Press Win+x and in the menu that opens, select “Windows Terminal (Admin)”:

If you have two versions of PowerShell installed on your OS (PowerShell 5 preinstalled with Windows, and PowerShell 7 installed manually), this will open PowerShell 5.

2. Run PowerShell as an administrator from the Start menu

To open PowerShell 7 as an administrator, click the Start menu and search for “PowerShell” or “PowerShell 7”, select “Run as Administrator”:

3. How to run Windows Terminal with administrator rights in Windows

By default, Windows Terminal uses PowerShell, so if you run Terminal with administrator rights, then Terminal will also be with elevated rights.

Remember that the terminal is not included in the standard set of Windows 10 programs and must be installed from the Microsoft Store. In Windows 11, this program is installed by default.

Go to the start menu and start typing “Terminal”:

If you do not see the “Run as administrator” item, then click the arrow to expand the entire list:

Click on “Run as administrator”.

4. How to run PowerShell as an administrator in the command line

If you already have a PowerShell console open with standard user rights, then directly from it you can start a new elevated PowerShell process:

Start-Process PowerShell -Verb RunAs

This command will launch PowerShell 5 or PowerShell 6, whichever is the default on your system. To run the latest version that you manually installed, i.e. PowerShell 7, run the command:

Start-Process pwsh -Verb RunAs

5. How to run PowerShell as an administrator on Linux

To run PowerShell as administrator on Linux, run the command with sudo:

sudo pwsh

How do I check if a PowerShell script or PowerShell terminal is running as administrator

If you need to run a PowerShell script as an administrator, you can check if the current process has powershell.exe or pwsh.exe elevated permissions right in your PS code.

In addition, sometimes you need to determine whether the terminal is running with elevated privileges or with the rights of a regular user.

How to determine if PowerShell terminal is running with administrator rights

First of all, pay attention to the command line prompt:

A PowerShell window opens with the current working directory in C:\windows\system32 and, accordingly, there is the following command line prompt:

PS C:\windows\system32>

If the PowerShell process has normal rights, then the user's folder is opened as the current working directory:

PS C:\Users\MiAl>

Things get more complicated if PowerShell is used through the Windows Terminal. Take a look at this screenshot, one of these terminals is open as an administrator, and the other as a regular user – they are no different.

It was a test of attentiveness – the name of the window with administrator rights contains the word “Administrator”.

When you need to find out if a given shell has administrator rights, run the following two commands in sequence:

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

PowerShell console with administrator privileges will print True, console with normal privileges will print False:

By the way, the same method can be used in scripts as well.

How to check in a PowerShell script that it is running as administrator

The following PowerShell code can be used to test if the current script is running in “Run as Administrator” mode:

Write-Host "Checking elevated rights..."
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
	Write-Warning "Insufficient permissions for the script. Open PowerShell console as administrator and rerun this script."
	Break
}
else {
	Write-Host "The code is run as administrator - script can continue..." -ForegroundColor Green
}

Save the PowerShell code in check_process_elevation.ps1 and run it in the console without admin rights:

C:\PS\check_process_elevation.ps1

As you can see, a message appeared that you do not have administrator rights, so the PowerShell script was stopped.

Now run the script in an elevated PowerShell session. As you can see, the script detected that this PowerShell session is running as administrator.

Alternatively, you can request administrator rights directly from a PowerShell script. To do this, instead of the line:

	Write-Warning "Insufficient permissions for the script. Open PowerShell console as administrator and rerun this script."
	Break

use the following code:

Start-Process Powershell -ArgumentList $PSCommandPath -Verb RunAs

When you run the script without administrator rights, it will rerun in a new elevated PowerShell session and you will see a UAC elevation prompt. If you accept it, your PS1 script will run as administrator. (The path to the current PowerShell script file is passed using the $PSCommandPath environment variable.)

In PowerShell 4.0 or newer, it's even easier to check if your script is running as an administrator. To do this, use the -RunAsAdministrator directive.

#requires -version 4.0
#requires -RunAsAdministrator
Write-Host "PowerShell is running as administrator" -ForegroundColor Green

If the script does not run under administrator, the following error will appear:

C:\PS\check_process_elevation.ps1: The script 'check_process_elevation.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.

If you run the script on a PowerShell v2 machine, you receive the following error message:

Cannot process the “#requires” statement at line 2 because it is not in the correct format.
The “#requires” statement must be in one of the following formats:
“#requires -shellid <shellID>”
“#requires -version <major.minor>”
“#requires -pssnapin <psSnapInName> [-version ]”

To manage Active Directory, you may need a different task: check if the current user has domain administrator rights from a PowerShell script. Use the following code:

If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Domain Admins"))
{
	# the user who ran this script has Domain Admins rights
}
Else
{
	# no Domain Admins rights
}

Leave Your Observation

Your email address will not be published.