Search notes:

admin.ps1

admin.ps1: A PowerShell script to become administrator (if the respective token is granted) or to query if the session is running with administrator privileges:
In order to query if I am running as adminstrator, the -amI or its alias -? must be specified.
PS C\:> admin -?
False
PS C\:> admin
PS C\:> admin -?
True

Source code

#
#  V.2
#
param (
   [alias('?')] [switch] $amI
)

set-strictMode -version 3

if ($amI) {

   $curIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
   $principal   = new-object System.Security.Principal.WindowsPrincipal $curIdentity
   $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)

   return
}

# Start same powershell executable as administrator
# in the current directory.
$ps_exe = (get-process -pid $pid).path
start-process $ps_exe -verb runAs -argumentList '-noExit', '-command', 'cd', "'$pwd'"
Github repository scripts-and-utilities, path: /admin.ps1

History

V.2 Start admin Powershell session in the current (working) directory (i. e. $pwd)

See also

admin.bat

Index