How to run PowerShell Script as admin?

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

PowerShell is a command-line shell that is used for administering windows OS and applications. Microsoft has built it using the .NET framework. CMDLETS are the command used by PowerShell to execute tasks. But while using PowerShell, you might often run into scenarios where you need administrator privileges to execute tasks. So, when I set up my PowerShell environment, I set up administrator privileges so that I can edit registry settings and do more complex tasks there. In this article, I will show you different solutions on how to run PowerShell script as admin.

Prerequisites

To follow all the steps mentioned in this article, you must have:

  • A Windows 10 PC
  • An account with administrator privileges
  • PowerShell

Solution 1: Search Bar

  • Firstly, go to the search bar of windows 10.
  • Search for PowerShell.
  • You will get the best match Windows PowerShell option.
  • Click on Run as Administrator.

 

 

  • You might get a popup, which will ask if you want to allow this app to make changes on your device, click on Yes.
  • The Windows PowerShell will start as Administrator.

 

Solution 2: Pinned Application

  • Firstly, go to the search bar of windows 10.
  • Search for PowerShell.
  • Click on the Windows PowerShell ISE application arrow.
  • Click on Pin to Start.

 

 

  • Now when you click on the start sign, you will see the Windows PowerShell ISE application pinned.

 

 

  • Right click on the application.
  • Click on more.
  • Click on Run as Administrator.

 

 

Solution 3: Taskbar

  • Go to the search bar of windows 10.
  • Search for PowerShell.
  • You will get the best match Windows PowerShell option.
  • Click on Pin to the taskbar.

 

 

  • The Windows PowerShell application will now appear on your taskbar.
  • Right click on the PowerShell taskbar application.
  • Click on Run as Administrator.

 

 

Solution 4: PowerShell Executable

Whether you are using a 32-bit or 64-bit operating system, you can run PowerShell as administrator from its respective location.

  • Go to the search bar of windows 10.
  • Search for PowerShell.
  • You will get the best match Windows PowerShell option.
  • Click on Open file location.

 

 

  • Select the powershell.exe file.
  • Right-click on powershel.exe and select Run as Administrator.

 

 

Solution 5: Shortcut

  • Go to the search bar of windows 10.
  • Search for PowerShell.
  • You will get the best match Windows PowerShell option.
  • Click on Open file location.

 

 

  • Select a PowerShell application and create a shortcut on the desktop.

 

  • Go to your desktop.
  • Right click on the PowerShell application.
  • Click on Properties.
  • Windows PowerShell Properties will open.
  • Click on Advanced.

 

 

  • Click on the Run as Administrator checkbox and click on Ok.

 

 

  • Finally, click on Apply and Ok.
    Now, whenever you run the PowerShell shortcut application on the desktop, it will run as administrator.

Solution 6: Run Command Window

The Run Command Window is a powerful tool that lets you run programs without searching for them using the Search Bar, Start Menu or File Explorer.

  • To run PowerShell as administrator by using the Run command window, press Win Key + R to open the run application.
  • Type powershell and press Ctrl+Shift+Enter.

 

 

This will open the PowerShell as administrator

Solution 7: Command Line

  • Go to the search bar of windows 10.
  • Search and select Command Prompt application.
  • Click on Run as Administrator.

 

 

  • Run this command in the command prompt -> powershell Start-Process powershell -Verb runAs
  • This will open Windows PowerShell as admin.
  • Note: Even if you don’t run command prompt application as administrator, this command will still open Windows PowerShell as admin.

Solution 8: Batch File

  • Open your favorite text editor.
  • Paste this line in the file -> Powershell.exe -Command “& {Start-Process Powershell.exe -Verb RunAs}”
  • Save it as a batch file with .bat extension as your preferred location.

 

 

  • Double click and run the batch file.
  • It will open a PowerShell window running as administrator.

Solution 9: Using Elevated Script

On UAC-enabled systems, to make sure a script is running with full admin privileges, add this code at the beginning of your script:

param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())

$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false)
{
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
Exit
}
'running with full privileges'

Now, when running your script, it will call itself again and attempt to elevate privileges before running. The -elevated switch

prevents it from repeating if something fails.

So, those were the multiple ways how you could run the PowerShell script as admin.

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.