Skip to content

Instantly share code, notes, and snippets.

@javafun
Forked from mh-firouzjah/better_powershell.md
Created April 2, 2023 12:15
Show Gist options
  • Save javafun/8764e48496fcb472fe6b560d00dd1e5d to your computer and use it in GitHub Desktop.
Save javafun/8764e48496fcb472fe6b560d00dd1e5d to your computer and use it in GitHub Desktop.
how to add zsh-like theme and feature to windows powershell

Better PowerShell on windows

Theme | History | KeyBinding

in order to make windows powershell more comfortable and add a zsh-like theme and feature follow me:

  • Run powershell as an administrator and execute following command to disable the execution policy, otherwise it wont let you follow next steps

    • Set-ExecutionPolicy RemoteSigned
    • Register-PSRepository -Default # this registers the default repository for PowerShell modules, so we can install packages thats are in next steps
    • Install-Module posh-git -Scope CurrentUser
    • Install-Module oh-my-posh -Scope CurrentUser
    • Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck
  • Now you need to create a profile to set custom settings for powershell, run the following caommand:

    • notepad $PROFILE # this would create/edit file as: "C:\Users<YOURUSERNAME>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
    • Now copy the following code and past it in the notepad and save the file.
  • You may need to install a font that supports the emojis!, I suggest to use Hack Nerd Font Mono

    • after downloading the font and install it, right click on powershells top bar and go to properties, click on Font tab and select the font. ** if youre using Windows Terminal:**
  • open the app, hit Ctrl and , then click on gear button at down-left side, inside the open jason file search for "profiles":, now edit/add "defaults": the fontFace so that finally sould looks:

...
"profiles": 
    {
        "defaults": 
        {
            "fontFace": "Hack Nerd Font Mono",
            ...
        },
        ...
    ...

** Read more at: **

Profile Code:

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme Powerlevel10k_Classic
Set-PSReadLineOption -PredictionSource History
Set-PSReadlineKeyHandler -Key "Tab" -Function MenuComplete
Set-PSReadlineKeyHandler -Key "UpArrow" -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key "DownArrow" -Function HistorySearchForward
Set-PSReadLineOption -Colors @{ InlinePrediction = '#898c5b'}
Set-PSReadlineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key "RightArrow" -ScriptBlock {
       param($key, $arg)

       $line = $null
       $cursor = $null
       [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

       if ($cursor -lt $line.Length) {
           [Microsoft.PowerShell.PSConsoleReadLine]::ForwardChar($key, $arg)
       } else {
           [Microsoft.PowerShell.PSConsoleReadLine]::AcceptNextSuggestionWord($key, $arg)
       }
}

Set-PSReadLineKeyHandler -Key End -ScriptBlock {
       param($key, $arg)

       $line = $null
       $cursor = $null
       [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

       if ($cursor -lt $line.Length) {
           [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine($key, $arg)
       } else {
           [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion($key, $arg)
       }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment