Skip to content

Instantly share code, notes, and snippets.

@jchandra74
Last active August 20, 2024 20:27
Show Gist options
  • Save jchandra74/5b0c94385175c7a8d1cb39bc5157365e to your computer and use it in GitHub Desktop.
Save jchandra74/5b0c94385175c7a8d1cb39bc5157365e to your computer and use it in GitHub Desktop.
PowerShell, Cmder / ConEmu, Posh-Git, Oh-My-Posh, Powerline Customization

Pimping Up Your PowerShell & Cmder with Posh-Git, Oh-My-Posh, & Powerline Fonts

Backstory (TLDR)

I work as a full-stack developer at work. We are a Windows & Azure shop, so we are using Windows as our development platform, hence this customization.

For my console needs, I am using Cmder which is based on ConEmu with PowerShell as my shell of choice.

Yes, yes, I know nowadays you can use the Linux subsystem on Windows 10 which allow you to run Ubuntu on Windows. If you are looking for customization of the Ubuntu bash shell, check out this article by Scott Hanselman.

For source control, I use git for my spikes since our main source control at work is still using hosted TFS on Visual Studio Team Services.

VS Code

My editor of choice nowadays is VS Code which is lightweight, customizable and all around a good environment to do development for AngularJS, Angular, NodeJS and other spikings in general. I've also done some .NET Core developments on the editor which is supported quite well including debugging if you follow that style of development. VS Code customization is a large topic by itself, so I won't be writing about it here... Perhaps some other time...

If you want to be on the cutting edge, you can try Visual Studio Code Insider. From my experience so far, it is pretty stable and is my VS Code environment of late.

For now, we'll focus on customizing Cmder, Powershell with git, posh-git, oh-my-posh and the Powerline fonts, specifically on how I setup mine which will net you something like the figure below.

Customized Cmder PowerShell with Oh-My-Posh agnoster theme

A lot of people has written up about customizing PowerShell but I can only find bits and pieces. This article hopefully will combine those bits and pieces into a more coherent end-to-end story.

Prerequisite: Install PowerShellGet

If you are already on Windows 10, you can skip this section.

If not, you will need to make sure you have PowerShellGet installed. We will use it to pull PS Modules from PS Gallery. Instruction on how to get PowerShellGet can be found here.

Upgrading to PowerShell v5 is preferable since it comes with PowerShellGet. The Get WMF 5 route from the link above will do this for you. Just make sure to download the right version for you OS. I.e. if you are on Win2K8R2, choose Win8.1AndW2K12R2-KB3134758-x64.msu from the Download list, etc.

You can check your PowerShell version by typing the following from within PowerShell.

$host.Version

Installing Chocolatey

Start PowerShell using Run As Administrator option. We might need it until we get everything installed.

I use Chocolatey as my install manager. To install Chocolatey, do the following from within PowerShell:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force

PowerShell might complain. If so, you probably need to change RemoteSigned to ByPass or Unrestricted. Running Get-ExecutionPolicy from inside PowerShell will tell you what to change it to.

Afterward, run the following and wait for it to finish:

iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

Installing Git using Chocolatey

If you already have git installed, you can skip this part.

If you do not know if git has been installed or not, you can check by typing the following into PowerShell:

git --version

If you have it installed, you should see the version of git that has been installed on your system.

If it is not installed, run the following to install Git from PowerShell via Chocolatey:

cinst git.install -y

Installing Cmder using Chocolatey

If you already have Cmder installed, you can skip this part.

Otherwise, run the following to install Cmder from PowerShell:

cinst cmder -y

Install Powerline Fonts

Clone the powerline repository from GitHub.

git clone https://github.com/powerline/fonts.git

afterward, do the following:

cd fonts
.\install.ps1

Wait for all the fonts to be installed.

From this point onward, you can switch to Cmder and do the rest from there.

Cmder Customization

Powerline Font Setup

Open up Cmder Settings dialog (Win+Alt+P when Cmder window is active) and ensure that one of the Powerline fonts is used for both Main console font and Alternative font as shown below. The Alternative font is needed for showing the correct icons for git on the PowerShell prompt.

Here I am using the Meslo LG M for Powerline font which I think looks quite nice. I tried using Ubuntu Mono for Powerline, but for some reason, it messed up my icons.

Try different fonts yourself and see which one you like. Just remember that you will need to choose the font that has been patched for Powerline, otherwise you Git icons might not show correctly.

Setting Up Fonts in Cmder

Pay attention that I also highlighted the ConEmu.xml location in the Storage field. You will need this location later when we customize the ConEmu Theme. It's not necessary but it's a nice to have. Also, yours might be located in a different directory than mine since I installed Cmder manually previously.

Quake Style Setup

Another thing that you might want to customize is to put Cmder into Quake style where you can show and hide it using Ctrl+~ key. To do that set the Quake mode setting to something like this:

Quake style setting

I like the animation speed set to 150 milliseconds since it is a bit snappier than the default 300 milliseconds.

PowerShell as Default Task Setup

To set PowerShell as your default task when you open Cmder, ensure that it is set as Specified named task as per figure below.

PowerShell as default task

Take note of the profile.ps1 location above inside the Selected contents section. I already changed this to the $PROFILE variable from inside PowerShell. By default, Cmder is pointing to its own profile.ps1 location. If you don't change this, your PowerShell profile customization that we will do later will not be loaded automatically upon launching Cmder or any new PowerShell task tab inside Cmder.

To check where your own PowerShell profile location, you can type $PROFILE inside PowerShell.

$PROFILE location

If you are not familiar with Windows, %USERPROFILE% is equivalent to C:\Users\username (which is the default user profile location in Windows, unless you have moved it somewhere else).

Also, you might have this file already or not. It does not matter. We'll create one or edit existing one later. The important thing here is that you need to point the PowerShell task to the right location or your profile customization will not load automatically and you will have to load it manually by typing the following into PowerShell:

. $PROFILE

_It's the equivalent of source .bashrc or source .zshrc, etc. if you are familiar with Linux.

In any case, it is a nice thing to remember when you further customize your $PROFILE.

To customize your profile you can run ise $PROFILE from PowerShell. Again, we will do this later, don't bother with it now.

To change that selected task contents, you can do it from the Tasks tab like the figure below. Ensure that you change both PowerShell and PowerShell as Admin.

PowerShell Tasks

ConEmu Theme Configuration

I am using one of the default themes that comes with ConEmu / Cmder which is Twilight.

Color settings

If you want to add other themes, you can clone the following git repository: https://github.com/joonro/ConEmu-Color-Themes and follow his instruction on How To Install it.

As I mentioned previously, pay attention to the Storage field setting in the figure above. You need to replace the -ConfigPath value to that particular ConEmu.xml location.

To clone the repository do:

git clone https://github.com/joonro/ConEmu-Color-Themes.git

Afterward, go to the tools directory like shown below:

cd .\ConEmu-Color-Themes\tool

ConEmu Theme Installation Script

And run the script to install a particular theme. The theme is located outside the tool directory.

ConEmu Themes

.\Install-ConEmuTheme.ps1 -ConfigPath C:\Users\jimmy\Downloads\cmder\vendor\conemu-maximus5\ConEmu.xml -Operation Add -ThemePathOrName ..\oceans16-dark.xml

In the figure about I am using an alias l which I bind to Get-ChildItemColorFormatWide from this git repository. Don't worry if you don't have it. Just do ls for now. I'll show you how to install that cmdlet a bit later.

Install Posh-Git and Oh-My-Posh

Posh-Git (PowerShell Git) will provide you with additional information on your PowerShell prompt when you are in a directory that has been designated as a local git repository.

Prior to installing these module, run the followings:

Install-PackageProvider NuGet -MinimumVersion '2.8.5.201' -Force
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name 'posh-git'

To install it, run the following from PowerShell:

Install-Module -Name 'posh-git'

Oh-My-Posh will let you theme your PowerShell prompt.

To install it, run the following from PowerShell:

Install-Module -Name 'oh-my-posh'

If PowerShell complains that you cannot do this, the most likely problem is that you do not have PowerShell v5 and PowerShellGet is not installed correctly. Revisit the step to do this above if that is the case.

To test the new Oh-My-Posh, do the followings from inside PowerShell:

Import-Module 'posh-git'
Import-Module 'oh-my-posh'
Set-Theme agnoster

If you've done everything correctly, your PowerShell prompt should like something like the figure below.

Agnoster theme

If something is off, like you are seeing weird characters, etc. It is probably due to you misconfiguring the Powerline font inside Cmder setting. If so, revisit the section above to configure it.

If you don't like the agnoster theme, you can set it to something else. Visit the Oh-My-Posh GitHub repository to see what other themes are available and additional instructions.

Get-ChildItemColor Customization

The following customization will colorize the directory listing. To install Get-ChildItemColor, simply run the following in PowerShell.

Install-Module -Name 'Get-ChildItemColor'

Once installed, you can use these cmdlet Get-ChildItemColor and Get-ChildItemColorFormatWide to list your directory content and it will colorize files, directory, etc. appropriately.

In the next section we will add alias so we can use l and ls as shortcuts to the cmdlet above.

Configuring the PowerShell $PROFILE

Run ise $PROFILE from within PowerShell. This will launch the Integrated Scripting Environment for PowerShell and let you edit your $PROFILE file.

Add the following things to that content of the $PROFILE file.

# Ensure that Get-ChildItemColor is loaded
Import-Module Get-ChildItemColor

# Set l and ls alias to use the new Get-ChildItemColor cmdlets
Set-Alias l Get-ChildItemColor -Option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -Option AllScope

# Helper function to change directory to my development workspace
# Change c:\ws to your usual workspace and everytime you type
# in cws from PowerShell it will take you directly there.
function cws { Set-Location c:\ws }

# Helper function to set location to the User Profile directory
function cuserprofile { Set-Location ~ }
Set-Alias ~ cuserprofile -Option AllScope

# Helper function to show Unicode character
function U
{
    param
    (
        [int] $Code
    )
 
    if ((0 -le $Code) -and ($Code -le 0xFFFF))
    {
        return [char] $Code
    }
 
    if ((0x10000 -le $Code) -and ($Code -le 0x10FFFF))
    {
        return [char]::ConvertFromUtf32($Code)
    }
 
    throw "Invalid character code $Code"
}

# Ensure posh-git is loaded
Import-Module -Name posh-git

# Start SshAgent if not already
# Need this if you are using github as your remote git repository
if (! (ps | ? { $_.Name -eq 'ssh-agent'})) {
    Start-SshAgent
}

# Ensure oh-my-posh is loaded
Import-Module -Name oh-my-posh

# Default the prompt to agnoster oh-my-posh theme
Set-Theme agnoster

The comments above should be self-explanatory.

Done

That's it. You should now have a pimped up PowerShell in Cmder that will let you know what's going on with the current git repository, a nicer prompt and fully colorized. Enjoy!

Testing if Powerline is correctly installed in Cmder

  1. Add the following function (stole from Michael Naumov) to your powerline $PROFILE. To edit your $PROFILE, just run ise $PROFILE from within PowerShell:
function U
{
    param
    (
        [int] $Code
    )
 
    if ((0 -le $Code) -and ($Code -le 0xFFFF))
    {
        return [char] $Code
    }
 
    if ((0x10000 -le $Code) -and ($Code -le 0x10FFFF))
    {
        return [char]::ConvertFromUtf32($Code)
    }
 
    throw "Invalid character code $Code"
}
  1. Run the following script inside PowerShell:
. $PROFILE
Write-Host "$(U 0xE0B0) $(U 0x00B1) $(U 0xE0A0) $(U 0x27A6) $(U 0x2718) $(U 0x26A1) $(U 0x2699)"

You should see something like the second line in the figure below: Git Symbols

If you don't see the symbols, please make sure you did the Powerline font installation and Cmder customization as described in the related section above.

References

@jfamousket
Copy link

Thanks @jchandra74

@fastboyz
Copy link

I think that everything is fine, but I get this http://prntscr.com/nn4pvu
all the other characters show just fine

@KennyYe
Copy link

KennyYe commented Aug 2, 2019

How did you get your background to appear below your powerline. When I set an image background, it seems to mess with the powerline. Any ideas?

@jchandra74
Copy link
Author

@KennyYe I am not using any background set in Cmder. The background in the screenshot is basically my Windows desktop background. I have the console to be transparent (from Features/Transparency)

@Gixx
Copy link

Gixx commented Oct 17, 2019

Hi,

your tutorial works like charm, it also works beautifully in the PHPStorm. One small question. Where and how can I change the prompt to exclude the user@host part? It's always me on the same Windows PC, no need to remind myself to that, and also I'd like save a bunch of wasted space...

@zawnk
Copy link

zawnk commented Oct 24, 2019

@Gixx

I'm not super familiar with the stuff, but my guess would be to edit the agnoster theme file. In there is a line where it says "$user@$computer ". I assume you can remove the @$computer and then it should be gone after you did . $PROFILE. I hope this works :)

@MajhiRockzZ
Copy link

MajhiRockzZ commented Oct 25, 2019

Step 1: Open PowerShell and Try 👇

$DefaultUser = 'Username'

image

Step 2: If it works then open Windows PowerShell ISE using this command👇

ise $PROFILE

image

Step 3: Edit Microsoft.PowerShell_profile.ps1 file by adding this line👇

Set-Theme Paradox
$DefaultUser = 'Username'

image

Step 4 : Click Save and Run icon 👇

image

Now restart your terminal 😍 (This will work for Agnoster, Fish, Honukai, Paradox and Sorin themes)

If by default Microsoft.PowerShell_profile.ps1 is not opened in Windows PowerShell ISE you can manually open the file by navigating to 👇

Documents\WindowsPowerShell directory.

I hope this was helpful ❤

@Gixx
Copy link

Gixx commented Oct 25, 2019

Thank you guys the answers. I will try them.

xoxo

@Gixx
Copy link

Gixx commented Oct 25, 2019

@zawnk, @MajhiRockzZ your solutions work like charm. Thank you very much!

@Pratham82
Copy link

Thank you very much man, This is exactly what I was needed.

@Bluesman74
Copy link

I can't seem to get either of the two Consolas Powerline fonts I found to work in Cmder

https://github.com/runsisi/consolas-font-for-powerline/blob/master/Powerline%20Consolas.ttf

or

https://github.com/ashwin/vim-powerline/tree/develop/font

At one point I seemed to manage to get the Tree symbol to show, but now I've again swapped between the fonts I can't get it.

Unfortunately their Font names are identical so I can't try both at once
One of them does give me , but the commits behind or ahead

@thardy
Copy link

thardy commented Apr 15, 2020

How do you set the startup dir using PowerShell? When I follow the recommended instructions Tasks->select task, then click "Startup dir..." button, it prepends the following:

-new_console:d:C:\dev *PowerShell (...everything else the same)

Which gives the following error when I launch it:

'*PowerShell' is not recognized as an internal or external command, operable program or batch file.

@lucknaumann
Copy link

@Bluesman74 commented on Mar 5, 2020, 10:32 AM MST:

I can't seem to get either of the two Consolas Powerline fonts I found to work in Cmder

https://github.com/runsisi/consolas-font-for-powerline/blob/master/Powerline%20Consolas.ttf

or

https://github.com/ashwin/vim-powerline/tree/develop/font

At one point I seemed to manage to get the Tree symbol to show, but now I've again swapped between the fonts I can't get it.

Unfortunately their Font names are identical so I can't try both at once
One of them does give me , but the commits behind or ahead

You might want to try Nerd fonts. If I'm not mistaken they're patched for Powerline. I don't see Consolas in the main NF repo but there's one I use on occasion -- can't recall how well it worked as I've noticed that some patched fonts don't work as well as others. I also don't use vim.
nerd-fonts
my-nerd-fonts (this is actually just consolasNF)

Or do a search of all GitHub for something like "powerline fonts consolas" and find a newer version. The ones you linked to are from 2013 so they may not work as well as newer versions. Here are just a few:
ConsolasForPowerline
consolas-powerline (is also for vim)
consolas-powerline-vim

@intractabilis
Copy link

Documents folder is not necessarily a %USERPROFILE% subfolder, moreover, its name is not necessarily Documents. %USERPROFILE%\Documents is bound to fail.

@lucknaumann
Copy link

lucknaumann commented May 24, 2020

@thardy commented on :

How do you set the startup dir using PowerShell? When I follow the recommended instructions Tasks->select task, then click "Startup dir..." button, it prepends the following:

-new_console:d:C:\dev *PowerShell (...everything else the same)

Which gives the following error when I launch it:

'*PowerShell' is not recognized as an internal or external command, operable program or batch file.

I had this same error. The full message I got was:

'*PowerShell' is not recognized as an internal or external command,
operable program or batch file.

Current directory:
%USERPROFILE%

Command to be executed:
"C:\WINDOWS\system32\cmd.exe" /C *PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. ''%USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'''"

ConEmuC: Root process was alive less than 10 sec, ExitCode=1.
Press Enter or Esc to close console...

If you look at the full error you'll see the problem under "Command to be executed:"
change the -new_console:d:C:\dev *PowerShell (...everything else the same) to *-new_console:d:C:\dev PowerShell (...everything else the same) (ie. move the asterisk in front of PowerShell to start of line). If you have "Invoke-Expression 'Import-Module ' instead of "Invoke-Expression '. ' that will also work (at least both did for me).

Edit: I noticed this will work if the Admin shell is set as startup shell but in "Create new console dialog" you have to tick the "Run as Administrator" box. To avoid this add -new_console:a to the beginning of the string. Eg: *-new_console:a -new_console:d:%USERPROFILE% PowerShell (...everything else the same). I left the asterisk at the beginning but not sure this is necessary.

references:
https://stackoverflow.com/a/37992402
https://conemu.github.io/en/NewConsole.html#syntax
https://conemu.github.io/en/LaunchNewTab.html#Create_new_console_dialog

@seanalexander
Copy link

Love this tutorial.

@amin131
Copy link

amin131 commented Oct 21, 2020

hello
Can anyone tell me why the icons are not displayed correctly?
git

@lucknaumann
Copy link

@amin131 commented on Oct 21, 2020, 3:37 PM MST:

hello
Can anyone tell me why the icons are not displayed correctly?
git

You need Nerd Fonts or Powerline Fonts. See my post from April 15, 2020, above for some links. I recommend Nerd Fonts as many also include Powerline Fonts.

@amin131
Copy link

amin131 commented Oct 24, 2020

@amin131 commented on Oct 21, 2020, 3:37 PM MST:

hello
Can anyone tell me why the icons are not displayed correctly?
git

You need Nerd Fonts or Powerline Fonts. See my post from April 15, 2020, above for some links. I recommend Nerd Fonts as many also include Powerline Fonts.

I install those fonts but it still doesn't show the icons! :(
Is there any solution ?

@lucknaumann
Copy link

lucknaumann commented Oct 24, 2020

@amin131
Did you make sure to select one of those fonts in Cmder's font section in Settings? Some fonts may work better than others in displaying those icons but that's the solution. They aren't displaying because whatever font you have selected doesn't contain those icons. They're basically Tofu. Also, make sure you installed the fonts for All Users. Sometimes that makes a difference.
Here's the Nerd Font repo for a wider selection:
https://github.com/ryanoasis/nerd-fonts

@AlessandroDiGioacchino
Copy link

AlessandroDiGioacchino commented Nov 3, 2020

Why isn't mine as colorful as yours? (As you can see, commands and environment variables aren't highlighted)
is it because I'm on Windows 7?

image

—EDIT—
Ok, fixed it.
Basically on Windows 7 the expression PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. "%USERPROFILE%\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1"'" -new_console:d:"%USERPROFILE%" defaults to PowerShell v5.1
By using C:\Program Files\PowerShell\7\pwsh.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. "%USERPROFILE%\Documents\PowerShell\Microsoft.Powershell_profile.ps1"'" -new_console:d:"%USERPROFILE%", I managed to have Cmder load PowerShell v7 instead (pay attention to the different path of the PowerShell profile as well)

@cyberbiont
Copy link

As far as I can tell, oh-my-posh themes don't work well with latest ConEMU versions, disrupting some of ConEMU functionality. For example very useful feature - being able to freely move cursor with mouse click - doesn't work with oh-my-pish enabled, bacause ConEMU requires special profile.ps setup (prompt function, see https://conemu.github.io/en/PowershellPrompt.html) which doesn't play well with posh-git. If you prove me wrong, I'd be happy.

@gzapata2
Copy link

gzapata2 commented Jul 4, 2021

How can I get this profile to work with Cmder instead of PowerShell?

this i would like also to know

@gautremf
Copy link

Just a heads up. oh-my-posh has changed the Set-Theme prompt to Set-PoshPrompt -Theme agnoster
Here is the link

@maxtaq
Copy link

maxtaq commented Mar 29, 2022

@amin131 Did you make sure to select one of those fonts in Cmder's font section in Settings? Some fonts may work better than others in displaying those icons but that's the solution. They aren't displaying because whatever font you have selected doesn't contain those icons. They're basically Tofu. Also, make sure you installed the fonts for All Users. Sometimes that makes a difference. Here's the Nerd Font repo for a wider selection: github.com/ryanoasis/nerd-fonts

I installed the powerline font in the Windows Font folder and specified "Anonymous Pro for powerline" in the WSL2 Terminal settings and it worked. Thank you!

@hannt99
Copy link

hannt99 commented Apr 22, 2022

After I did the step "Configuring the PowerShell $PROFILE", I exited Cmder and then reopened it after that I've got this error
How do I fix it?
image

@ArtemCK
Copy link

ArtemCK commented Apr 24, 2022

ConEmu64_IU4VKHiaGN

To make a persistent change to an environment variable on Windows using the System Control Panel:

Open the System Control Panel.
Select System.
Select Advanced System Settings.
Go to the Advanced tab.
Select Environment Variables....
Make your changes.

SystemPropertiesAdvanced_tFJ8DbYfg4

@hannt99
Copy link

hannt99 commented Apr 25, 2022

I've solved this problem Start-SshAgent in PROFILE file doesn't work for me. I changed it to Start-Service ssh-agent and it works

@Hrushal-Nikhare
Copy link

I found a way to init oh-my-posh in cmder without changing to cmd

Steps

  1. Open Cmder
  2. Run clink info
  3. open the Scripts directory with cmder in it
  4. make a new file called oh-my-posh.lua
  5. Insert the following into it
    load(io.popen('oh-my-posh init cmd'):read("*a"))()
  6. Reload andddd Done!

@g-kon
Copy link

g-kon commented Oct 26, 2023

Hi, tried following your guide today but unfortunately the images you've included within the .md file are no longer visible, so there's no way to follow this guide unless the images are re-introduced (or using this wayback machine url: https://web.archive.org/web/20230207031803/https://gist.github.com/jchandra74/5b0c94385175c7a8d1cb39bc5157365e - which gets the job done but is still not ideal). Thought I'd just mention it! And of course thanks for taking the time to write this guide!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment