Functions/Test-IsAdmin.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function Test-IsAdmin { <# .SYNOPSIS Determines if you are running elevated (as Administrator) .DESCRIPTION Determines if you are running elevated (as Administrator) .EXAMPLE Test-IsAdmin Would return the following if the prompt was elevated. $True .EXAMPLE Test-IsAdmin -Verbose Would return the following if the prompt was elevated. VERBOSE: You have Administrator rights. True .NOTES #source https://blogs.technet.microsoft.com/heyscriptingguy/2015/07/29/use-function-to-determine-elevation-of-powershell-console/ # inspired by above, made some minor modifications like proving more output, making it an advanced function. #> #region Parameter [CmdletBinding(ConfirmImpact = 'None')] [OutputType('bool')] Param() #endregion Parameter begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { if ( -not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { Write-Verbose -Message "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!" Write-Output -InputObject $False } else { Write-Verbose -Message 'You have Administrator rights.' Write-Output -InputObject $True } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |