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.RawUI
if((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 admin
function 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:\windows\system32\notepad.exe
Clear-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 john@savilltech.com.