DEV Community

loading...
Cover image for My powershell configuration

My powershell configuration

dialguiba profile image Diego Updated on ・5 min read

Preview

At the end of this guide your terminal will be like this.
preview

Index

  1. Installing Windows Terminal (Optional)
  2. Installing or Updating Powershell
  3. Installing OhMyZsh for Windows OhMyPosh
  4. Installing and Configuring Fonts
  5. Enabling Sudo Command
  6. Installing module for icons
  7. Some color personalization for Readline

Installing Windows Terminal

First of all, I recommend you install Windows terminal that could will allow you personalize your terminal even more. You can download it from the microsoft store.

Download Windows Terminal

Installing or Updating Powershell

(This is not strictly necessary but i recommend you install it if you can so you wouldnt have problems with any step)
Install the last version of powershell. You can download it from here. Remember that this version is going to replace the powershell 6 if you have installed it. (Does not affect powershell version 5)

Download Powershell 7

Installing OhMyZsh for windows OhMyPosh

You need to use the PowerShell Gallery to install oh-my-posh. (Refered: https://github.com/JanDeDobbeleer/oh-my-posh) You can see the themes available there. But take into account that some themes dont have the same names that appear in the repository.

Install posh-git and oh-my-posh:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
Enter fullscreen mode Exit fullscreen mode

Enable the prompt:

# Start the default settings
Set-Prompt
# Alternatively set the desired theme:
Set-Theme Agnoster
Enter fullscreen mode Exit fullscreen mode

In case you're running this on PS Core, make sure to also install version 2.0.0-beta1 of PSReadLine

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck
Enter fullscreen mode Exit fullscreen mode

To enable the engine edit your PowerShell profile:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE
Enter fullscreen mode Exit fullscreen mode

In the file opened copy these lines so next times you open terminal it will load the modules and theme

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox
Enter fullscreen mode Exit fullscreen mode

Some Useful Commands:

To see what are your actual settings:

$ThemeSettings

If you want to open the file that define your profile in powershell.

$profile

It gives you the path of your configuration profile path. You can open it with the command notepad or code if you have visual studio code.

notepad $profile

To see what are the names of the available themes:

get-theme

If you want to test some themes temporaly you can use the command:

Set-Theme NameofTheTheme

To see what are the actual colors of your theme:

Show-ThemeColors

To see what are the colors available that you can use:

Show-Colors

Remember that when you choose a theme and want to see it every time you start powersheel you have to define it in your profile file using

notepad $profile
Set-Theme NameOfTheme

Installing and Configuring Fonts

If you dont install the fonts you probably will see something like this:

nofonts

So, you should install a font that includes glyphs. Two of them are Cascadia Code PL or MeslolGS NF

MesloLGS NF Regular
You can install it with double click

Cascadia Code

Now, to set the fonts in terminal:

configterminalfonts

in that configuration file oyu have to add configuration for you default profile or specifically for your powershell 7

configscommand

Also, If you have want to use that version of powershell and set that font in vscode to use it in the terminal you could do this.

  • Pwshell as default shell

defaultshell1

Select the version 7 of powershell

defaultshell2

  • add this lines in settings.json

"terminal.integrated.fontFamily": "MesloLGS NF"

Enabling Sudo Command (Optional)

Also if you want to have the sudo command like in linux you should download an executable and save it in a folder in your pc and then add that folder in your environment variables path. (You can save that file in c:/Windows folder too. Taking advantage that it is in the environmental variable path) Well, this is optional, you always can change properties of powershell to force it open as administrador always. But, this would be necessary for you if you want to execute something as administrator in ps6 when you are coding in vscode. (You can solve that just open it as administrator too. haha.)

Sudo command

Installing module for icons

(Referred: https://github.com/devblackops/Terminal-Icons)

To install the module from the PowerShell Gallery:

Install-Module -Name Terminal-Icons -Repository PSGallery

To use it modify your profile file adding this line:

Import-Module -Name Terminal-Icons

Note: Take into account that this module can make it take a bit longer to start powershell.

You can see your file with icons using:

Get-ChildItem | Format-List

Get-ChildItem | Format-Wide

Get-ChildItem | Format-Wide -Column 3

Enter fullscreen mode Exit fullscreen mode

Aditionally, if you want to use ls and la commands in powershell so you can see your files with icons easily you could add these lines in your profile file

#ALIAS

Set-Alias -Name l -value Get-ChildItem
Set-Alias -Name ls -value lsformatwide
Set-Alias -Name lsl -value lsformatlist

#FUNCIONES

function lsformatwide {
    Param(
        [switch]$la
    )

    if ($la) {
        Get-ChildItem $Args[0]. -Force  | Format-Wide -Column 3
    }
    else {
        Get-ChildItem $Args[0] | Format-Wide -Column 3
    }
}
function lsformatlist {
    Get-ChildItem | Format-List
}
Enter fullscreen mode Exit fullscreen mode

Now you can use l,ls,lsl to get different views of your files. Also if you want to see hidden files you can use -la after ls

Some color personalization for Readline

Some color personalization for Readline

I have additional configuration for colorising the readline words adding these lines in my profile.ps1

Set-PSReadLineOption -Colors @{
    Command            = 'Magenta'
    Number             = 'DarkGray'
    Member             = 'DarkGray'
    Operator           = 'DarkMagenta'
    Type               = 'DarkCyan'
    Variable           = 'Green'
    Parameter          = 'DarkGreen'
    ContinuationPrompt = 'DarkGray'
}
Enter fullscreen mode Exit fullscreen mode

Discussion (12)

Collapse
seefer007 profile image
Darren Evans

Powershell on my laptop and desktop PC point to the same .ps1 file on my OneDrive. I created two oh-my-posh themes, one for my desktop that does not display battery status and one for my laptop that does display battery status.

How do I load a theme depending on whether it's my laptop running PowerShell or my desktop if PowerShell on both my machines is pointing to the same .ps1 $profile in OneDrive? I suspect I need to have my laptop and desktop PowerShell $profile variable pointing to a file stored locally on the respective machine. How do I do this?

Collapse
dialguiba profile image
Diego Author

Hello, why dont you create a conditional in your profile that could load a different theme depending on information about the system?. I think that you can retrieve information about your system using get-computerInfo.
docs.microsoft.com/en-us/powershel...

Collapse
gbursson profile image
Greg R.

Is everything ok with Sudo command link?

Collapse
dialguiba profile image
Diego Author • Edited

Hello! yes. I use it :)
image

Collapse
gbursson profile image
Greg R.

There is no host in the URL...

Thread Thread
dialguiba profile image
Diego Author

I am sorry. I didn't notice that.
Here it is github.com/dialguiba/PowerShellCon...

Collapse
jandedobbeleer profile image
Jan De Dobbeleer

Hey Diego. First off, thanks for using oh-my-posh. I just dropped in to let you know we’ve been working on V3, which supports every shell you’ll find on Windows and more. Check the website for more information. ohmyposh.dev/

Collapse
dialguiba profile image
Diego Author

Thank you so much. I am gonna see it. Amazing work!

Collapse
therajuahammad profile image
Raju Ahammad

How to remove this?

Collapse
dialguiba profile image
Diego Author • Edited

Hello, have you recently installed it? If i am not bad, the new version have an easier way to change the themes than before. In the theme's folder you can modify the json of your theme or you can create another. It contains what you want to show. Which theme are you using?

Collapse
therajuahammad profile image
Raju Ahammad

Hi,
Thanks for your reply. Yes, I installed it recently. I'm using "agnoste" theme. Where is the folder location of theme JSON?

Thread Thread
dialguiba profile image
Diego Author • Edited

At the end of this page. It explains you how to personalize a theme in powershell. ohmyposh.dev/docs/installation
And here you have some of the things that you can add to your theme.
ohmyposh.dev/docs/configure
I would like to test it but i dont have windows right now 😔
Hope it helps you

Forem Open with the Forem app