Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
PowerShell

How to Run PowerShell Scripts with Administrative Privileges

Sooner or later, as you begin to hone your PowerShell skills, you’ll start writing scripts to automate repetitive tasks. If you run your workstation with standard user privileges, you’ll soon discover that it’s not possible to launch PowerShell scripts with administrative privileges by right-clicking the script and selecting Run as administrator from the context menu (which is available for most over types of executable). Today I’ll show you two ways that you can launch PowerShell scripts with admin privileges.

Modify a Script to Force Elevation

Add this snippet of code to the beginning of your PowerShell script, and a UAC prompt will appear, asking for administrative credentials or consent before any subsequent code is executed.

​ param([switch]$Elevated)
function Check-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false)  {
if ($elevated)
{
# could not elevate, quit
}

else {

Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
Run a PowerShell script with administrative privileges
Running a PowerShell script with administrative privileges.

Run a Code from an Elevated Instance of the Windows PowerShell Integrated Scripting Environment (ISE)

Alternatively, you can run scripts directly from inside the Windows PowerShell ISE. To start the ISE with administrative privileges:

  • Switch to the Start menu in Windows 8, type powershell ise, and make sure that PowerShell ISE is selected in the search results. Press CTRL+SHIFT+ENTER to start the ISE with elevated privileges and enter administrative credentials or give sent if prompted.
  • In the PowerShell ISE window, select Open from the File menu to load your script.
  • Once the script is loaded into the ISE, press F5 to run the script.

The Windows PowerShell ISE is a useful environment for creating and editing your scripts. You have access to all the installed PowerShell modules and their related commands, plus troubleshooting tools.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (2)

2 responses to “How to Run PowerShell Scripts with Administrative Privileges”

  1. <p>Hello I'm new to Petri and I'm also new to PowerShell. I'm running a PowerShell to setup proxy server and create new registry keys.</p><p>I'm able to to accomplish both jobs, but I have to run them in two separate scripts, the one for the proxy without admin permissions,</p><p>the one to create the registry keys needs to run as admin.</p><p>I want to run both scripts, how can accomplish this task.</p><p><br></p><p>Thank you,</p>

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.