Custom PowerShell config using oh-my-posh themes and posh-git.
It is recommended to use PowerShell 7.6.4 for improved performance, cross-platform compatibility, and access to the latest features. You can download and install it from the official PowerShell GitHub repository or install it directly from the default powershell on your computer using
winget upgrade --id Microsoft.PowerShellAfter installation, ensure that PowerShell 7.6.4 is your default terminal by setting it in your system's terminal settings.
Prior to installing anything, you will need to allow PowerShell to run Scripts. Check the script execution policy setting by executing
Get-ExecutionPolicyIf it is not set to unrestricted, use the following line to change the status :
set-ExecutionPolicy unrestricted -Scope CurrentUserThis will be usefull for posh-git as well as for running conda commands in PowerShell.
Your PowerShell profile is a file that stores commands or scripts which will be executed when opening a PowerShell session. To create it, run the following command:
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}Then to access your PowerShell profile, type :
notepad $PROFILEThis will open your profile file in a text editor allowing you to edit it.
PowerShell 7.6.4 may detect modules from both the legacy Windows PowerShell 5.1 directory and its own module directory. To ensure PowerShell 7.6.4 uses its own modules, follow these steps:
- Add the following line to prioritize PowerShell 7.6.4 modules:
$ps7ModulesPath = "C:\program files\windowsapps\microsoft.powershell_7.6.4.0_x64__8wekyb3d8bbwe\Modules"
$env:PSModulePath = "$ps7ModulesPath;$env:PSModulePath"- Save the file and restart PowerShell 7.6.4.
- Verify the module path order:
$env:PSModulePath -split ";"- Ensure the PowerShell 7.6.4 path appears first.
By default in PowerShell, command names, parameter names, argument values and file paths can all be completed by pressing the Tab key. Using right arrow will also complete the whole command line based on previous inputs. However, there is not default configuration to auto-complete only 1 word at a time.
Adding the following line to the Profile will solve this issue:
Set-PSReadLineKeyHandler -Chord "Ctrl+RightArrow" -Function ForwardWordTo enable history-based command predictions in PowerShell 7.6.4, add the following to your profile file:
Set-PSReadLineOption -PredictionSource HistoryYou can also customize the prediction view style to either Inline or List:
Set-PSReadLineOption -PredictionViewStyle Inline # or ListAfter saving the file, restart PowerShell 7.6.4. As you type, PowerShell will suggest commands from your history that match the current input. Use the → (Right Arrow) key to accept the suggestion or ↓ (Down Arrow) to cycle through available suggestions.
oh-my-posh is a custom theme engine that can be used on any Shell - including PowerShell. To install it, instructions are available on their website.
First, install oh-my-posh using :
winget install JanDeDobbeleer.OhMyPosh -s wingetThen restart the terminal. If oh-my-posh is not recognized as a command, you can run the installer again, or add it manually to your PATH (for example: $env:Path += ";C:\Users\user\AppData\Local\Programs\oh-my-posh\bin).
For your PowerShell to run oh-my-posh everytime you load it, you will need to add the following snippet in the Profile file :
oh-my-posh init pwsh | Invoke-ExpressionThen reload your profile using . $PROFILE.
posh-git is a PowerShell module that integrates Git and PowerShell by providing Git status summary information that can be displayed in the PowerShell prompt.
Installation guidelines are provided in the readme of posh-git repository. First check that the prerequisites are met. Then run (if posh-git has never been installed before) :
Install-Module posh-git -Scope CurrentUserThen to automatically load posh-git everytime PowerShell is opened add the following line to the Profile file :
Import-Module posh-gitThen reload your profile using . $PROFILE. Now git tab completion should be enabled.
For conda to run in PowerShell, you will need to run the following line :
conda init powershellThen restart PowerShell. Note conda won't be functionnal is the ExecutionPolicy is not set to Unrestricted.
By default, conda will autoactivate itself, when the terminal is opened. If you prefer not, then disable auto-activation with:
conda config --set auto_activate_base falseWith the above instructions, a basic PowerShell theme has been set. But oh-my-posh allows for endless customisation possibilities. Below are a few things that can be done to improve the PowerShell theme.
A large amount of themes are available with oh-my-posh. They can be used either by downloading .json files and loading them through the Profile file or by loading themes through oh-my-posh github repository. All instructions are described in oh-my-posh's documentation.
Here, only the way using a json file is described. First browse through the available themes and download the json for the one you chose (e.g. stelbent-compact.minimal)
Then place the downloaded .json in a dedicated folder, for instance C:Users/user/Posh. Then add the following snippet to the Profile file :
oh-my-posh init pwsh --config 'C:/Users/user/Posh/stelbent-compact.minimal.withcondaenv.omp.json' | Invoke-Expressionoh-my-posh init pwsh | Invoke-Expression that was previously added to the Profile file.
Now reload your profile and the theme should be updated.
By default, python .venv won't necessarly be added to your PowerShell config. Luckily this can be changed by modifiying the .json used for the PowerShell theme. All instructions for creating custom configurations with oh-my-posh are listed here.
Adding the following block at the end of the segment will allow to display the python version and .venv that is currently used :
{
"type": "python",
"style": "powerline",
"powerline_symbol": "\ue0b0",
"foreground": "#100e23",
"background": "#906cff",
"template": " {{ .Full }} {{ if .Venv }}{{ .Venv }}{{ end }}",
"properties": {
"fetch_virtual_env": true
}
}
See the theme file in the repo to know where to place this block in the segments.
The default font in PowerShell won't readme most icons - such as the python icon for instance. To fix this, nerd fonts can be downloaded. For instance the CaskaydiaCove Nerd Font can be downloaded here
To install the font, unzip the downloaded folder, then open it, right click on the font you want to install and click 'install'.
Then, it is needed to select this font in the PowerShell setting file. To do that, open the terminal and click on the little down facing arrow on the upper part of the terminal. Then hit 'settings'. Then go to '> Windows PowerShell > Appearance' and chose the desired font.
