Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Check If a PowerShell Script Is Run As Administrator?

January 15, 2020 PowerShell

How to Check If a PowerShell Script Is Run As Administrator?

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

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

Write-Host "Checking for elevated permissions..."
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "Insufficient permissions to run this script. Open the PowerShell console as an administrator and run this script again."
Break
}
else {
Write-Host "Code is running as administrator — go on executing the script..." -ForegroundColor Green
}

PowerShell script to Check for Elevated Admin Rights

Save the PowerShell code to the check_process_elevation.ps1 file and run it in the console without the administrator privileges:

C:\PS\check_process_elevation.ps1

As you can see, the message appeared that you have no administrator privileges, so the PowerShell script has been stopped.

not elevated powerhell session

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

check if a powershell process is running as administrator (elevated)

Also you can request elevation right from the PowerShell script. To do it, instead of the string:

Write-Warning "Insufficient permissions…”

use the following code:

Start-Process Powershell -ArgumentList $PSCommandPath -Verb RunAs

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

uac promt to elevate powershell script

In PowerShell 4.0 or newer, it is even easier to check if your script running with the administrator privileges. To do it, use the –RunAsAdministrator directive.

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

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

The script ‘check_process_elevation.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.
At line:1 char:1
+ C:\PS\check_process_elevation.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (check_process_elevation.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation

powershell ScriptRequiresElevation

If you run the script on a computer with PowerShell v2, the following error message will appear:

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 <major.minor>]”

To manage Active Directory, you may need another task: to check if the current user has the domain admin privileges from a PowerShell script. Use the following code:

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

0 comment
1
Facebook Twitter Google + Pinterest
previous post
Fixing Windows Update and DISM Error 0x80073712 on Windows Server 2016/Windows 10
next post
How to Manage NTFS Permissions with PowerShell?

Related Reading

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Delete Old User Profiles in Windows?

January 25, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • How to Create a UEFI Bootable USB Drive to Install Windows 10 or 7?
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Deploy PowerShell Active Directory Module without Installing RSAT
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top