Skip to content

Instantly share code, notes, and snippets.

@SystemJargon
Created September 26, 2022 02:34
Show Gist options
  • Save SystemJargon/1ee35ad6673e9884659cfc982995abc6 to your computer and use it in GitHub Desktop.
Save SystemJargon/1ee35ad6673e9884659cfc982995abc6 to your computer and use it in GitHub Desktop.
Info about when running powershell scripts and force require it be run as an Administrator to execute.

Purpose: When running powershell scripts and force require it be run as an Administrator to execute.

Simple check

#Requires -RunAsAdministrator

More detailed / older powershell version

function Test-Administrator  
{  
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
}

If (Test-Administrator = True) {
    Write-host "Powershell is executed with Administrative privileges"
    }
Else {
    Write-host "This script needs to be executed with Administrative privileges, exiting"
    exit
    } 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment