Skip to content

Instantly share code, notes, and snippets.

@cahilfoley
Last active June 14, 2019 13:19
Show Gist options
  • Save cahilfoley/175b14fb08c50bb283a7748c423ebe96 to your computer and use it in GitHub Desktop.
Save cahilfoley/175b14fb08c50bb283a7748c423ebe96 to your computer and use it in GitHub Desktop.
[Powershell] Usefully scripts when working with Powershell #powershell #admin
#########################################################
# Ensure PS script is running with elevated permissions #
#########################################################
# Works in Powershell >= v4.0, add to top of file
#Requires -RunAsAdministrator
# Works in all versions of powershell, add to top of file
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Output "This script needs to be run As Admin"
Break
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment