We all like to have our customization in our Windows Desktop. Custom colors, icons, wallpaper, etc.. Well IT guy/gal, why not have your PowerShell CLI the same way? I’ve looked around at a few blogs and got some ideas to share with you on customizing your PowerShell CLI.
Now, by default, Windows looks in the following directory for your customization file:
C:\Users\(username)\Documents\WindowsPowerShell
It looks for a file called profile.ps1 and will load that script every time you load PowerShell once it is customized.
You can construct the script within PowerShell ISE or your favorite editor. The following is how I programmed the script to customize my PowerShell prompt:
First, we want to clear the slate on the PowerShell CLI.
I like to add my contact information for instance.
1 2 3 4 5 6 7 | #Set the PowerShell CLI for customization. #Written by Lance Lingerfelt / LDLNET LLC - lance.lingerfelt@ldlnet.net - http://www.ldlnet.net #Start Script# #Clear the default PowerShell Window Clear-Host |
Second, I run a script that I came across and added to my customization.
It get’s me the weather for the local area that I am from. Click the link for details.
How To Uniquify Your PowerShell Console (Scroll to Getting the Weather)
1 2 3 | #Run the Script Get-Weather.ps1 to get the weather forcast. . $PSScriptRoot\Get-Weather.ps1 Get-Weather -City 'Charlotte' -Country 'USA' |
Next, I customize the PowerShell Window Settings so that it is the size and shape that I want.
I set the Directory Location, The Colors, and The Window Sizes.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #Customize the PowerShell Window Settings set-location c:\PowerShell $a = (Get-Host).UI.RawUI $a.BackgroundColor = "black" $a.ForegroundColor = "white" $size = $a.WindowSize $size.Width = 200 $size.Height = 50 $a.WindowSize = $size $size = $a.BufferSize $size.Width = 200 $size.Height = 9999 $a.BufferSize = $size |
Next, I wanted to write some text in the window before I get my PowerShell Prompt.
I work at Avanade, so I wanted to put a welcome message, today’s date, and what PS version I was running just because I could.
1 2 3 4 5 6 7 | #Customize the text to be shown before the prompt $time = Get-Date $curUser= (Get-ChildItem Env:\USERNAME).Value $psVersion= $host.Version.Major Write-Host "Welcome to Avanade PowerShell, $curUser!" -foregroundColor Green Write-Host "Today is: $($time.ToLongDateString())" -foregroundColor Yellow Write-Host "You're running PowerShell version: $psVersion" -foregroundColor Red `n |
Lastly, we actually configure the prompt. There is a lot of ways to do this and I will leave references for you at the bottom of this post so that you can get more details on the commands actually run.
I wanted to have a colorful prompt that stated the company motto and put the current time.
I also configured the Window Title at the top to show text, the current user, and the current directory with different colors as that is my expressive part of my brain. 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #Customize the actual PS prompt function prompt { Write-Host -NoNewLine "Avanade... " -foregroundColor Red Write-Host -NoNewLine "Know, Share, Do: " -foregroundColor White Write-Host -NoNewLine "[" -foregroundColor Yellow Write-Host -NoNewLine (Get-Date -f "hh:mm:ss tt") -foregroundColor Cyan Write-Host -NoNewLine "]" -foregroundColor Yellow Write-Host -NoNewLine ">" -foregroundColor Red $host.UI.RawUI.WindowTitle = "Avanade PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)" Return " " } #End Script# |
Here is the script in its entirety:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #Set the PowerShell CLI for customization. #Written by Lance Lingerfelt / LDLNET LLC - lance.lingerfelt@ldlnet.net - http://www.ldlnet.net #Start Script# #Clear the default PowerShell Window Clear-Host #Run the Script Get-Weather.ps1 to get the weather forcast. . $PSScriptRoot\Get-Weather.ps1 Get-Weather -City 'Charlotte' -Country 'USA' #Customize the PowerShell Window Settings set-location c:\PowerShell $a = (Get-Host).UI.RawUI $a.BackgroundColor = "black" $a.ForegroundColor = "white" $size = $a.WindowSize $size.Width = 200 $size.Height = 50 $a.WindowSize = $size $size = $a.BufferSize $size.Width = 200 $size.Height = 9999 $a.BufferSize = $size #Customize the text to be shown before the prompt $time = Get-Date $curUser= (Get-ChildItem Env:\USERNAME).Value $psVersion= $host.Version.Major Write-Host "Welcome to Avanade PowerShell, $curUser!" -foregroundColor Green Write-Host "Today is: $($time.ToLongDateString())" -foregroundColor Yellow Write-Host "You're running PowerShell version: $psVersion" -foregroundColor Red `n #Customize the actual PS prompt function prompt { Write-Host -NoNewLine "Avanade... " -foregroundColor Red Write-Host -NoNewLine "Know, Share, Do: " -foregroundColor White Write-Host -NoNewLine "[" -foregroundColor Yellow Write-Host -NoNewLine (Get-Date -f "hh:mm:ss tt") -foregroundColor Cyan Write-Host -NoNewLine "]" -foregroundColor Yellow Write-Host -NoNewLine ">" -foregroundColor Red $host.UI.RawUI.WindowTitle = "Avanade PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)" Return " " } #End Script# |
Here is the final product when opening PowerShell:
HAPPY SCRIPTING!
HAPPY VALENTINES DAY!
References:
Customizing your PowerShell Profile
Get-Weather.ps1
Modify your PowerShell Prompt
PowerShell Basics: Console Configuration