3

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 Answers 3

4

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
              
Sign up to request clarification or add additional context in comments.

3 Comments

It worked thanks for the solution. My administrator window needed to use the correct font as you also mentioned.
Even in Visual Code you can enter the Nerd font in settings (Preferences -> Settings (search for Font Family and add Nerd font name in Terminal section) and the terminal window will show the correct icons instead looking broken.
Thanks, @codingjoe - the linked docs cover that too, but I've updated the answer to make that clearer.
1

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.

7 Comments

Where is the global profile.ps1 located? It gives a true - but I cannot find it. Only under my own user.
I found it by writing the $PROFILE.AllUsersCurrentHost out: C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
Thanks for the suggestion but I am getting other errors in the admin prompt window now: Get-PSReadLineKeyHandler : A parameter cannot be found that matches parameter name 'Key'. At line:465 char:43 + if ((Get-PSReadLineKeyHandler -Key Spacebar).Function -eq ... + ~~~~ + CategoryInfo : InvalidArgument: (:) [Get-PSReadLineKeyHandler], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.GetKeyHandlerCommand
I found this post telling that the path for oh-my-posh should be different. stackoverflow.com/questions/75499007/…
I am not getting any errors anymore, but theme is still not applied for my admin
|
0

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.

2 Comments

Yes, I have tried to configure a more general -ALL USERS approach, but the profile file is not present for admin. I can only see my user in the '/users/' folder. My question is how to configure it for all or for the admin as well
For a given user, there is no difference in $PROFILE locations between running non-elevated vs. elevated (verify with $PROFILE | Select *).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.