Customizing PowerShell terminal with oh-my-posh v3
Setting up oh-my-posh v3 custom theme for PowerShell
Last year I wrote an article on how to Customizing WSL2 on Windows with screenfetch and oh-my-zsh as I was spending a lot of time in WSL2 doing some heavy development for Linux platform using .NET Core. However recently I found myself working a lot in PowerShell prompt mainly with Git and really missed the customization I did for WSL, so I decided to customize my PowerShell console as well.
I found that oh-my-posh rleased a new version in the meanwhile and there are some other PowerSehll Modules like Terminal Icons you can use to customize your terminal environment and make it more easier and more eye appealing to work work with it.
Even thou oh-my-posh and Terminal Icons have quite good documentation on how to set them up for your PowerShell terninal profile, some people have troubles putting all configuration in place. For this reason I decided to write this article that takes you to setting up and customizing your terminal profile step by step starting from the scratch.
I went one step further with PowerShell customization and I created my own theme based on pr-existing one from oh-my-posh themes list.
This customization is done with PowerShell 7. I barely use old PowerShell 5 which is replaced by Microsoft in 2016 with PowerShell Core which is practically PowerShell 6. If you haven't switch to new version I suggest you do so as it a lot more flexible and cross-platform as well https://aka.ms/powershell-release?tag=stable
Upgrading to oh-my-posh v3 for PowerShell
If you are not new to oh-my-posh and you happen to already use oh-my-posh v2 than you can just simply follow the guide for upgrading from v2 from oh-my-posh official website. Simple run of the following command will upgrade your current version
Update-Module -Name oh-my-posh -Scope CurrentUser
However you still may want to stick around and read the article to the end as I will cover some additional customization if you want your PowerShell prompt to look like the image above.
Setting up oh-my-posh for PowerShell from the scratch
OK you are completely new to this and you want to have your terminal look like the one above. You can go to official page of oh-my-posh and find the guide how to set it up, but I found that even some of my friends who are IT savvy had troubles and some problem setting up and customizing their PowerShell terminal profiles.
I use my PowerShell session from Windows Terminal which is now available from Windows Store, so you can easily install it from there. For me this is the most convenient way of using it, so I'll continue explaining how to setup oh-my-posh to work properly from Windows Terminal, although I will also mention how to have this in place if you are not using windows terminal and you start your PowerSehll terminal using cmd or directly from shortcut on your Windows 10 environment.
Now that you have your Windows Terminal installed, before we even proceed to settin up oh-my-posh, you will need to install one of the Nerd Fonts. I prefer Cascaydia Cove Nerd Font which you can download from the Nerd Fonts download list or directly from the link here. This Font is required so that icons used by oh-my-posh and Terminal Icons are displayed properly.
Once you download the archive containing the fonts from Cascaydia family, select them all and install via right click. It will take just few seconds and you should have it available in pretty much all aplications on your system. Next step is to set it as font for the PowerShell terminal from Windows Terminal, so launch Windows Terminal and go to settings.
On the left side you'll see list of all terminal environment you have available on your machine. Select PowerShell and in the right panel go to Appearance section. Here you will be able to select CascaydiaCove Nerd Font so that all icons used in oh-my-posh themes are displayed properly.
Installing oh-my-posh and setting the theme
We have Nerd Fonts in place and we can proceed to installing and configuring oh-my-posh. So start your PowerShell session from Windows Terminal and install oh-my-posh theme with the following command
Install-Module oh-my-posh -Scope CurrentUser
When the command execution is done, you will have your module installed but you are still not using it and you won't notice any change in the terminal. To do this, let's first picha theme to start using it
I really like nu4a theme which I used as a starting point for my custom theme, so I set that one initially
Set-PoshPrompt -Theme nu4a
Your PowerShell terminal looks cool already, but there are few more things you need to do. This setting is only for the current session, and you do no really want to use the module by typing in the command every time you start new session.
We need to save our current theme and add usage of it to the session start. To save the current theme setting to a JSON file use the following command
Export-PoshTheme -FilePath ~/.mytheme.omp.json -Format json
This will take your current theme settings and store it on the disk in your user/home folder which well deal later to customize the theme.
Once your theme is saved you need to configure PowerShell session to start with using this theme in oh-my-posh module. To do this you need to open your profile configuration file. You can do it dirctly from the current session using notepad or Visual Studio Code
notepad $PROFILE # OR code $PROFILE
Once you open the profile file with your editor of choice, add the following line to the file and save it
Set-PoshPrompt -Theme ~/.mytheme.omp.json
Now, whenever you start new session, it will start with the theme settings from the JSON file.
Customizing oh-my-posh theme
We have all in place, but you may want to change some bits and pieces here and there in the current theme. Since we already have our theme stored locally in our $HOME folder, we can easily open it via Viasual Studio Code and start changing the colors, icons and other properties.
To open the theme file directly from the current terminal session just run the following line
Now I am not going to walk you through all the options ad settings of the theme, you can find that at oh-my-posh configuration page. However, one important thing you need to know is usage of icons.
If you want to change any of the icons, I suggest you use Nerd Font cheat sheet page where you can search for any icon available in Nerd Fonts and get it's Hex value and use that one in theme configuration JSON file.
When using hex value for icon in JSON configuration file make sure you prefix copied value from the cheat sheet page with \u. For example, if you want to use alert icon, you will get f421 hex value from the page, but in JSON file you use it as \uf421
You can download my customized theme JSON file from the download button in this article.
Setting up Terminal Icons
You probably noticed icons and colors in ls command at the very beginning of this article. This is done by Terminal Icons module which is pretty straight forward how to install. Al lyou need is to run the following command to install it on your local system from PowerShell Gallery.
Install-Module -Name Terminal-Icons -Repository PSGallery
Upon installation, module will still not be utilized by the PowerShell sessions on startup, so we need to go back to our profile file and add the following line right after oh-my-posh line.
Set-PoshPrompt -Theme ~/.mytheme.omp.json Import-Module -Name Terminal-Icons
This will initialize the modules on every PowerShell terminal session start.
Using on-my-posh and Terminal Icons with PowerShell initialized from shortcut or cmd
Once you apply your setting in the profile file, they will be available fro any Powershell session regardless you started it from Windows Terminal or from anywhere else. However icons may not work because Windows uses different default font for consoles.
This can be easily fixed by opening console setting dialog from the elevated instance of the console window (Run as Administrator)
Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
Comments for this article