Different profiles for elevated vs non-elevated instances

Find out a way to have a different profile for elevated vs non-elevated PowerShell instances.

John Savill

March 11, 2016

2 Min Read
Different profiles for elevated vs non-elevated instances

Q. How can I have different PowerShell profiles for an elevated vs non-elevated PowerShell instance?

 A. It may be desirable to have a different PowerShell profile depending on if its an elevated vs non-elevated instance. There is not a separate profile for elevated vs non-elevated however as an alternative you can put logic in the profile to check if the instance is elevated or not then run different portions of code.

To check if an instance is elevated an easy way is to check for the administrator SID (elevated):

(whoami /all | Select-String S-1-16-12288) –ne $null

This could then be used in a script. In this example I set the background to dark red and modify the prompt if elevated and if not a standard black background.

#Set-Location c:$shell = $Host.UI.RawUIif((whoami /all | Select-String S-1-16-12288) –ne $null) #if admin{$shell.WindowTitle="John's Admin PowerShell"$shell.BackgroundColor = "DarkRed"$shell.ForegroundColor = "White"#Set the prompt to say adminfunction prompt{$(if (test-path variable:/PSDebugContext) { '[DBG]: ' }else { "ADMIN PS [$env:COMPUTERNAME] " }) + $(Get-Location) `+ $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '} }else #not admin{$shell.WindowTitle="John's PowerShell"$shell.BackgroundColor = "Black"$shell.ForegroundColor = "White"}#Import-Module ....New-Item alias:np -value c:windowssystem32otepad.exeClear-Host

Each week, John Savill answers all of your toughest tech questions about the worlds of Windows Server, Azure, and beyond. Read his past IT advice here, and email your questions to [email protected].

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like