Beautify PowerShell on Windows 10

Gaopeng Bai
3 min readApr 7, 2020

As a Windows user, I often envy the high-end atmosphere of Linux users.

In fact, the PowerShell that comes with Windows is also very powerful.

But the overly rudimentary interface does not know how many users were dismissed, so let’s beautify PowerShell.

The following tutorial environment is PowerShell 5.x.

The final appearance is shown below

Configuration step by step

Install post git

In order to customize the theme, you must install it in the following way:

Install-Module -Name posh-git -AllowPrerelease -Force

If you don’t have an -AllowPrerelease flag, first upgrade PowerShellGet with

Install-Module -Name PowerShellGet -Force

If there is an issue about “can not running script”, by using this command to solve it.

Set-ExecutionPolicy RemoteSigned

Install DirColors

The ls command under Linux supports highlighting different files, and PowerShell must definitely not lack this feature.

We execute the following command to install and introduce DirColors:

Install-Module DirColors -Scope CurrentUser
Import-Module DirColors

Install oh-my-posh

Those who have used Linux have basically used the artifact oh-my-zsh, and PowerShell has a similar module oh-my-posh. oh-my-posh provides an interface similar to oh-my-zsh, with a full face value.

Execute the following command to install and introduce oh-my-posh:

Install-Module oh-my-posh -Scope CurrentUser
Import-Module oh-my-posh

Set theme (Agnoster, Sorin, Avit, robbyrussell, or customize):

Set-Theme robbyrussell

Install PSReadLine

With a beautiful shell, why not use syntax highlighting?

PSReadLine is a module that provides command line highlighting for PowerShell.

Use the following command to install and introduce PSReadLine:

Install-Module PSReadLine -Scope CurrentUser
Import-Module PSReadLine

Install fonts

Maybe when you tried other themes in the previous step, you found that some strange characters could not be displayed because the system lacked the corresponding fonts.

Execute the following code to install Powerline fonts:

git clone https://github.com/powerline/fonts.git
cd .\fonts\
.\install.ps1

After installation, execute the following code to delete extra files:

cd ..
Remove-Item -Force .\fonts\

Hin: The system needs to use utf8 to import all fonts. If this is not the case with your system settings, modify your system settings.

Screenfetch

One of the pictures that must be printed after Linux is installed is screenfetch. I can’t think of this kind of thing under Windows.

Execute the following command to install screenfetch:

Install-Module windows-screenfetch -Scope CurrentUser

After installation, you can directly enter Screenfetch to see the effect, which is very cool.

Configure PROFILE

Our beautification is basically complete, let us take a look at what we do after starting PowerShell:

Import-Module DirColors
Import-Module oh-my-posh

Set-Theme your-theme
Import-Module PSReadLine

Is n’t it cumbersome to do this every time? Fortunately, PowerShell provided us with PROFILE.

Type the following command in PowerShell:

$PROFILE

It is found that the directory of a file is output, open it directly with a text editor, enter the command of the module just imported, save and exit.

Profile

The backgound color set RGB as 88,88,88

Content of file:

# Dracula readline configuration. Requires version 2.0, if you have 1.2 convert to `Set-PSReadlineOption -TokenType`
Set-PSReadlineOption -Color @{
“Command” = [ConsoleColor]::Green
“Parameter” = [ConsoleColor]::Gray
“Operator” = [ConsoleColor]::Magenta
“Variable” = [ConsoleColor]::White
“String” = [ConsoleColor]::Yellow
“Number” = [ConsoleColor]::Blue
“Type” = [ConsoleColor]::Cyan
“Comment” = [ConsoleColor]::DarkCyan
}
# Dracula Prompt Configuration
Import-Module posh-git
$GitPromptSettings.DefaultPromptPrefix.Text = “$([char]0x2192) “ # arrow unicode symbol
$GitPromptSettings.DefaultPromptPrefix.ForegroundColor = [ConsoleColor]::Green
$GitPromptSettings.DefaultPromptPath.ForegroundColor =[ConsoleColor]::Cyan
$GitPromptSettings.DefaultPromptSuffix.Text = “$([char]0x203A) “ # chevron unicode symbol
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::Magenta
# Dracula Git Status Configuration
$GitPromptSettings.BeforeStatus.ForegroundColor = [ConsoleColor]::Blue
$GitPromptSettings.BranchColor.ForegroundColor = [ConsoleColor]::Blue
$GitPromptSettings.AfterStatus.ForegroundColor = [ConsoleColor]::Blue

Import-Module PSReadLine
Screenfetch
#region conda initialize
# !! Contents within this block are managed by ‘conda init’ !!
(& “C:\conda\Scripts\conda.exe” “shell.powershell” “hook”) | Out-String | Invoke-Expression
#endregion

#use PSReadLine only for PowerShell and VS Code

if ($host.Name -eq 'ConsoleHost' -or $host.Name -eq 'Visual Studio Code Host' ) {

#ensure the correct version is loaded

Import-Module PSReadline -RequiredVersion 2.2.0

#ListView currently works only with -EditMode Windows properly

Set-PSReadLineOption -EditMode Windows

if ($host.Version.Major -eq 7){

#only PS 7 supports HistoryAndPlugin

Set-PSReadLineOption -PredictionSource HistoryAndPlugin

}

else{

#use history as the prediction source on 5.1

Set-PSReadLineOption -PredictionSource History

}

#add background color to the prediction preview

Set-PSReadLineOption -Colors @{InlinePrediction = "$([char]0x1b)[36;7;238m]"}

#change the key to accept suggestions (default is right arrow)

Set-PSReadLineKeyHandler -Function AcceptSuggestion -Key 'ALT+r'

}

--

--