DEV Community

Cover image for Improving PowerShell 📟
Lucas Frazao
Lucas Frazao

Posted on

Improving PowerShell 📟

Hello, I'm Lucas Frazao. I've been working as a software engineer (SWE) for the past five years.

In this article, I want to present an alternative to customize and improve your PowerShell experience. But first, who is this post for?

  • First, for my darling, my love and my life;
  • Windows users;
  • Windows users who are not interested in or do not like WSL (Windows Subsystem for Linux);
  • People who want to learn more about the capabilities of PowerShell.

So, let's do it.

What is Powershell?

PowerShell is a cross-platform CLI (command-line interface), meaning it is a terminal application that can run on any operating system (OS), including Windows, Linux, and macOS. If you don't know PowerShell, it's okay. You can access the official documentation at this link or check out this video on the Microsoft Developer channel on YouTube: Introduction to PowerShell.

Every Windows operating system comes with PowerShell pre-installed, which you can access through the Windows menu by searching for "powershell".

searching powershell

Meantime, our goal is to improve the CLI experience on Windows. Let's do this in steps so you can achieve the main goal while understanding what is happening.

Install NerdFonts

First of all, we need to install a font with glyphs, because later we'll need this functionality.

Glyphs refer to shapes, designs, and specific representations of characters in a font or encoded character. In practice, they are characters that can represent letters, numbers, symbols, and even icons.

Access nerdfonts.com, select a font you like, and click "Download". Once the download is complete, navigate to the folder where the zip file was saved, unzip it, and install all the font styles.

install nerd font

image font: https://support.microsoft.com/en-us/office/add-a-font-b7c5f17c-4426-4b53-967f-455339c564c1

CLI Configuration

Let's start by downloading Windows Terminal, an application that allows you to run different terminals in separate tabs. This means you can open PowerShell in the first tab and cmd in the second tab.

To download, access the link Windows Terminal - apps microsoft or search for "Windows Terminal" in the Microsoft Store from your Windows menu. Click on "Download" to start the installation process. To confirm the installation, you can search for "terminal" in your Windows menu and you'll see something like this:

searching by terminal

Now, let's download the latest version of PowerShell. You might ask yourself, "Isn't PowerShell already native on Windows?" The answer is "Yes" but PowerShell 7+ is the latest and improved version. Below, we have a comparison between both versions and some references where you can get more information.

PowerShell
(Desktop Edition)
PowerShell
(Core Edition)
Interactions Version 1.0, 2.0, 3.0, 4.0, 5.0 e 5.1 Version 6.0 and 7
Compatibility Only for Windows Cross-platform, with support for macOS and Linux distributions like Ubuntu, Fedora, and CentOS
Runtime .Net Framework runtime $PSVersionTable.PSEdition defined by desktop. .Net Core runtime $PSVersionTable.PSEdition defined by core
Policy Updates Only critical bug fixes will be released. New features and bug fixes will be released.
Package Comes as a pre-installed component in Windows via WMF. Open-source application, can be installed via MSI, ZIP, or PKG on macOS.
Supported Script Environment Windows PowerShell Integrated Scripting Environment (ISE) Visual Studio Code (VSCode) with PowerShell extension

References: Differences between Windows PowerShell 5.1 and PowerShell 7.x and Difference Between Windows PowerShell And PowerShell Core.

To download PowerShell 7, follow the steps similar to the previous step. Besides accessing it via Windows, you can also access the Microsoft Store via browser: PowerShell 7 - Microsoft Store.

A brief disclaimer... from now on, whenever you read "PowerShell", we will be referring to PowerShell 7.x.

After installing PowerShell, we have completed the first step, and you will notice the visual difference between the two versions.

tab Windows powershelltab powershell
Windows PowerShell v5.1 - PowerShell 7.x

Setting CLI Default

Let's change the PowerShell execution policies and set it as the default CLI. Therefore, some commands will need to be executed as Administrator because we are changing "high-level" configurations, which are settings that a typical or non-technical user usually doesn't modify.

To start, we need to verify the current execution policies. Open Windows Terminal as Administrator, select PowerShell from the menu options, and run:
menu options profile

Get-ExecutionPolicy
Enter fullscreen mode Exit fullscreen mode

If you got something different from RemoteSigned, run the command below to set the policy to RemoteSigned:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Enter fullscreen mode Exit fullscreen mode

Link to learn more about execution policies: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.4#-executionpolicy

Great! Now, let's reload Windows Terminal to continue the settings. Press Ctrl + , and then click on "Open JSON file" at the bottom left corner.
Open JSON file

Now, your Windows Terminal configuration file will open in your code editor (or notepad). After this, let's verify the PowerShell ID to set it as the default profile. To make it easier, you can press Ctrl + F and search for Windows.Terminal.PowershellCore. The expected result should look something like this:

evidence powershell

Let's find "defaultProfile", this field stores the default terminal CLI profile.
evidence default profile

Copy the guid, then replace the defaultProfile with the copied value from the PowerShell guid. Save the file, reload Windows Terminal, and you will see that it starts with the PowerShell profile selected.

new tab powershell

Installing Oh My Posh

To customize the PowerShell visual, we'll use Oh My Posh, a custom prompt for any shell that offers many pre-built themes.

Run the command:

winget install JanDeDobbeleer.OhMyPosh -s winget
Enter fullscreen mode Exit fullscreen mode

This command will install and run the oh-my-posh.exe and add the latest theme by default. Now run:

// To open it in Notepad
notepad $PROFILE

// To open it in Visual Studio Code (recommended)
code $PROFILE
Enter fullscreen mode Exit fullscreen mode

You can run the commands using your favorite editor instead of Notepad (first command) or VS Code (second command). This command will open your PowerShell profile file.

If you got an error similar to Cannot find the ...$PROFILE file. Do you want to create a new file?, run the command:

New-Item -Path $PROFILE -Type File -Force
Enter fullscreen mode Exit fullscreen mode

This command will open a blank file named $PROFILE. After opening the profile file, add the line below:

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression
Enter fullscreen mode Exit fullscreen mode

This code creates a configuration that, when PowerShell starts, Oh My Posh will be opened, and the theme used will be jandedobbeleer as set up in the previous command.
If you encounter a script error while setting up a new rule for PowerShell instances, open the Windows Terminal as an administrator and run the command below. This will set your PowerShell execution policy to unrestricted.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Enter fullscreen mode Exit fullscreen mode

Link with explanation about execution policy: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.4#-executionpolicy

Then, reload your Windows Terminal, and when it opens, you can check the new "default" visual. It will look something like this:
powershell using oh my posh

If you want to use a different theme, you can check out Oh My Posh Themes or run the command below to list themes:

Get-PoshThemes
Enter fullscreen mode Exit fullscreen mode

To change the theme, simply access the profile file and replace the word jandedobbeleer with the name of the new theme chosen from the previous list. The example below shows how to change the theme from jandedobbeleer to spaceship.

Before:

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression
Enter fullscreen mode Exit fullscreen mode

After:

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\spaceship.omp.json" | Invoke-Expression
Enter fullscreen mode Exit fullscreen mode

You can make more granular changes, but it is highly recommended to read the official documentation first: Oh My Posh Documentation.

My recommendation for customization is to try the acrylic material. If you want it, press Ctrl + ,, follow up with Profiles -> Defaults -> Appearance, then enable the option "Enable acrylic material", like this:

menu to change translucent

After:
powershell after acrylic

Before:

powershell before acrylic

Adding Terminal-Icons

The Terminal-Icons is a PowerShell module that adds icons to folders and files when you navigate in the terminal.To install Terminal-Icon run the command:

Install-Module -Name Terminal-Icons -Repository PSGallery
Enter fullscreen mode Exit fullscreen mode

Add to profile the line below that manages the module import.

Import-Module -Name Terminal-Icons
Enter fullscreen mode Exit fullscreen mode

If you don't remember how to access the profile file, run the command code $PROFILE. Then, you'll get something like this:
profile file

After doing this, reload the Windows Terminal and run the command dir. You will then be able to see the Terminal-Icons in action.
terminal running dir command

To dive deeper into more customizations and changes, access the official documentation: Terminal-Icons Documentation.

Note: If instead of the custom icons shown above, you see an icon like ó°›‹, you need to change your default font in Windows Terminal to one of the fonts downloaded from nerdfonts.com, as mentioned earlier in this article.

Bonus - Adding the Posh-Git

Posh-Git is a module that integrates Git with PowerShell, providing additional information and functionalities related to Git. It is recommended to read the Posh-Git official documentation to understand the best ways to enhance your productivity when using it.

Remember, to use Posh-Git, you must have installed git previously on your machine beforehand.

Open the Windows Terminal as administrator and run the command bellow to install:

PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
Enter fullscreen mode Exit fullscreen mode

Add the line below on you profile file.

Import-Module posh-git
Enter fullscreen mode Exit fullscreen mode

Finally, if you want to further customize the appearance of your Windows Terminal, just use the appearance menu we saw in this article and let your creativity guide you. Remember that some settings may negatively affect your use of the tool.
So, you've run through important steps to customize PowerShell, update, and configure it. Now, your PowerShell looks much better than when we started, but it still has your unique flair. Explore the documentation and references to get more customizations, learn more about the power of Posh-Git, create aliases, and explore other functionalities in all installed modules. Feel free to send me feedback or share your opinion down below. See you.

Top comments (0)