PowerShell theme not working in administrator mode

284 Views Asked by At

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.

PowerShell in user vs 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?

3

There are 3 best solutions below

3
mklement0 On BEST ANSWER

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.

    • font configuration

  • 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:

        • In the profile's settings, make sure that 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
              
2
SJ the Sahil Joseph On

When working with PowerShell profiles and customizations like oh-my-posh, it's important to consider the differences between the regular user mode and administrator mode. If your configuration is working in regular user mode but not in administrator mode, it might be due to the different profiles used for each mode.

7
SJ the Sahil Joseph On

Open a PowerShell window as an administrator and run the following command to check if the global profile file exists:

Test-Path $PROFILE.AllUsersCurrentHost

If it returns False, the global profile file doesn't exist yet.

Create the global profile file by running the following command:

New-Item -Path $PROFILE.AllUsersCurrentHost -ItemType File -Force

Open the global profile file in any code/text editor.

Add the necessary configuration for oh-my-posh:

'oh-my-posh init pwsh --config C:\Users\Public\oh-my-posh\themes\jandedobbeleer.omp.json' | Invoke-Expression

Save the changes to the global profile file and close the text editor. Then, open a new PowerShell window as an administrator to see if the oh-my-posh theme is applied.