ADVERTISEMENT

How to Run PowerShell Script as Administrator?

When you open the PowerShell console on Windows 10 via 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 10 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. Just press Windows + X on your keyboard or right-click the Start button and select Windows PowerShell (Admin).

powershell runas

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.

  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;
    powershell elevate to admin
  3. Click the Advanced button and enable the Run as administrator option;
  4. Click OK twice.
    run powershell as admin
READ ALSO  Viewing Office 365 User Account Details Using Get-MsolUser

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 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 cmd.exe command prompt, PowerShell, or using the Run feature.

run powershell script as admin

Check if PowerShell is Running as Administrator

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

powershell script run as administrator

In some PowerShell scripts, it is sometimes necessary to verify that the script is running with administrator privileges 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 powershell script as administrator from cmd

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:

READ ALSO  Accessing Domain Controller from Local DSRM Account

The script ‘test_admin_script.ps1’ cannot be run because it contains a “#requires” statement for running as 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.

Cyril Kardashevsky

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.