$Host.UI.RawUI.BackgroundColor = 'DarkBlue' $Host.UI.RawUI.WindowTitle = $myInvocation.M圜ommand.Definition + '(Elevated)' # We are running as an administrator, so change the title and background colour to indicate this If ($myWindowsPrincipal.IsInRole($adminRole)) # Check to see if we are currently running as an administrator # Get the security principal for the administrator role
$myWindowsPrincipal = New-Object ($myWindowsID) # Get the ID and security principal of the current user account There a few minor issue with his code a modified version based on fixes suggested in the comment is below.īasically it gets the identity associated with the current process, checks whether it is an administrator, and if it isn't, creates a new PowerShell process with administrator privileges and terminates the old process. Benjamin Armstrong posted an excellent article about self-elevating PowerShell scripts.