I have managed to install the oh-my-posh theme for my PowerShell prompt; however, the theme is not working when I start the PowerShell prompt in admin mode.
I have tried to adjust the profile.ps file by moving the reference to the json file to a public folder
(C:\Users\MyUser\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1):
'oh-my-posh init pwsh --config 'C:\Users\Public\oh-my-posh\themes\jandedobbeleer.omp.json' | Invoke-Expression'
My assumption is that the admin user has access to the 'MyUser
' folder.
Maybe this is not the problem, and I am looking in the wrong place.
The normal PowerShell window works like a charm.
How can I make the oh-my-posh theme also work with the PowerShell prompt in administrator mode?
For a given user, there is no difference in $PROFILE
locations between running non-elevated vs. elevated (verify with $PROFILE | Select *
), so if your elevated sessions run with the current user identity, they load the same profile files.
However, your screenshot shows:
The calling, non-elevated session is a (properly oh-my-posh configured) Windows Terminal (WT) session.
By contrast, your elevated (run-as-admin) session is a regular console window (provided by conhost.exe
)
Unlike the WT session, this regular console window isn't configured to use a Nerd font, which is required in order for icons in oh-my-posh prompts to render properly - see the docs, which also cover how to configure fonts in Visual Studio Code and Visual Studio (use the relevant tabs).
Therefore, you have two options:
Configure your regular console windows to use the same font as your WT sessions:
Open the elevated window's system menu (via clicking the icon in the top left corner of the window) and choose either Properties
or Defaults
: the former configures settings for this window and all future windows with the same window title, the latter for all console windows that don't have custom settings.
As shown in the following screenshot, pick a Nerd font (the recommended one is shown), after which the icons in the prompt should render properly.
Preferably, make sure that elevated PowerShell sessions open in WT too:
Interactively, from inside WT:
Consider creating a dedicated profile that runs with elevation by default:
Run this profile as Administrator
is turned on.Ad hoc, when clicking on the dropdown list for opening a new tab with a specific profile, Ctrl-click (right-click) on the profile of interest, with presents a shortcut menu for launching the profile with elevation (as administrator).
Programmatically / from outside WT:
Unfortunately, at least as of Windows 11 22H2, even configuring your system to use WT by default (run start ms-settings:developers
, setting Terminal
) is not enough to start elevated sessions in WT - neither via the taskbar nor through programmatic invocation, e.g. via Start-Process -Verb RunAs
The following simple PowerShell helper function, psa
, can help: it launches an elevated PowerShell session in WT if the current session is also running in WT and works in both PowerShell editions; if you place it in your $PROFILE
file, it will be available in future PowerShell sessions.
# Launch an interactive elevated PowerShell session in WT if running in WT,
# invariably in a new window.
function psa {
if ($env:WT_SESSION) { Start-Process -Verb RunAs wt.exe "`"$((Get-Process -Id $PID).Path)`"" }
else { Start-Process -Verb RunAs (Get-Process -Id $PID).Path }
}
Note:
While this works for interactive elevated sessions launched by you, it doesn't prevent scripts from requesting elevation directly via Start-Process -Verb RunAs
.
In scripts you control, for invocation of elevated sessions with commands to execute, you can avail yourself of the Enter-AdminPSSession
function available from this Gist:
Assuming you have looked at the linked Gist's source code to ensure that it is safe (which I can personally assure you of, but you should always check), you can install Enter-AdminPSSession
directly as follows; after installation, run it with -?
to get help:
irm https://gist.github.com/mklement0/f726dee9f0d3d444bf58cb81fda57884/raw/Enter-AdminPSSession.ps1 | iex