Contents

Custom PowerShell Profile

Overview

A custom powershell prompt can help with your day to day productivity and can easily be customised to your needs. This post shows how this can be achieved using some very cool modules and a custom PowerShell profile.

Learn about PowerShell profiles here.

Requirements

I’ll follow an example using my own custom profile. The profile will:

  • display a message of the day.
  • load daily functions.
  • load daily modules, including those used for the prompt and theme.
  • show the current weather.
  • display git status.
  • display session elevated status and PowerShell version.
Module Description
Posh-Git Used to display git status in PowerShell prompt
oh-my-posh Used as the theme engine for the PowerShell prompt
PoshLog PoShLog is powershell cross-platform logging module. This replaced my own logging functions

Create or Open the Custom Profile

Create a custom profile if it does not exist. I am using the CurrentUserAllHosts profile so that my profile is consistent. Remember to check out the docs for more information on each profile.

1
2
3
4
5
6
7
8
# Create or open a custom profile: ----------------------------------------------------
if (Test-Path $Profile.CurrentUserAllHosts) {
  New-Item $Profile.CurrentUserAllHosts -Force -Confirm:$false 
} else {
  # Open it up in your default text editor: -------------------------------------
  Invoke-Item $Profile.CurrentUserAllHosts
}

Now copy the code as below or change/add to suit your own needs…

 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

# SHELL CUSTOMISATION: --------------------------------------------------------
# COMMON MODULES: -------------------------------------------------------------
# Import the modules and install if not available.
if (Get-Module -ListAvailable -Name PoshLog, PoshLog.Enrichers) {
    Import-Module PoshLog, PoShLog.Enrichers, posh-git, oh-my-posh
} else {
    Install-Module PoshLog, PoShLog.Enrichers, posh-git, oh-my-posh
    Import-Module PoshLog, PoShLog.Enrichers, posh-git, oh-my-posh
    Import-Module PoshLog

} # END if (Get-Module -ListAvailable -Name PoshLog) 

# Set the theme for oh-my-posh: -----------------------------------------------
Set-PoshPrompt -Theme Paradox

# HELPER FUNCTIONS: -----------------------------------------------------------
# Test if session is elevated.
function Test-RunAsAdministrator {
    $Principal = [Security.Principal.WindowsPrincipal] ([Security.Principal.WindowsIdentity]::GetCurrent())
    if ($Principal.IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
        return $true

    } else {
        return $false

    } # END if ($Principal.IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator"))
} # END function Test-RunAsAdministrator

# Add the ability to retrieve type accelerators - not sure why this was removed in later versions.
function Get-TypeAccelerators {
  [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get

}

# SET-LOCATION:----------------------------------------------------------------
$Params = @{
	Name = "git"
	PSProvider = "FileSystem"
	Root = "d:\git"
	Description = "Git Folder"
}

[void](New-PSDrive @Params)
Set-Location git:

# MOTD BANNER: ----------------------------------------------------------------
$Context = $env:USERNAME + '@' + $Env:COMPUTERNAME
$SystemInfo = Get-CimInstance -Class CIM_OperatingSystem
$LastBootUpTime = $SystemInfo.LastBootUpTime
$Time = (Get-Date) - $LastBootUpTime

# Weather:
# Review https://github.com/chubin/wttr.in for docs and options and adjust web request
# with your code.
# Check if running in the ISE:
if ($psIse) {
    # ISE displays incorrectly so use a one-liner format.
    $Weather = (Invoke-WebRequest wttr.in/xxxx?format=3).Content
} else {
    $Weather = (Invoke-WebRequest wttr.in/xxxx?0AQFn).Content
} # END if ($psIse)

# Display the banner: 
$MOTD = @"    
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
█░▄▄▀█░▄▄▄██▄██░███▄██▄░▄█░██░███░▄▄▀█░▄▄█░▄▄░█░██░██▄██░▄▄▀█░▄▄█░▄▄███░▄▄█░▄▄▀█░▄▄█░▄▄█▄░▄█░██░██
█░▀▀░█░█▄▀██░▄█░███░▄██░██░▀▀░███░▀▀▄█░▄▄█░▀▀░█░██░██░▄█░▀▀▄█░▄▄█▄▄▀███▄▄▀█░▀▀░█░▄██░▄▄██░██░▀▀░██
█▄██▄█▄▄▄▄█▄▄▄█▄▄█▄▄▄██▄██▀▀▀▄███▄█▄▄█▄▄▄████░██▄▄▄█▄▄▄█▄█▄▄█▄▄▄█▄▄▄███▄▄▄█▄██▄█▄███▄▄▄██▄██▀▀▀▄██
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
$Context | $($SystemInfo.Caption) $($SystemInfo.OSArchitecture)
PowerShell Version : $($host.version.ToString())
Uptime : [ $($Time.Days) Days, $($Time.Hours) Hrs, $($Time.Minutes) Mins ]

$Weather

"@
Write-Host $MOTD -ForegroundColor DarkCyan

# Set the terminal title if elevated: -----------------------------------------
# oh-my-posh will also alter your prompt to reflect admin status.
if (Test-RunAsAdministrator) {
    $Host.UI.RawUI.WindowTitle = "Admin! PowerShell $($host.version.ToString())"
} else {
    $Host.UI.RawUI.WindowTitle = "PowerShell $($host.version.ToString()) "
}

Profiles are useful to display immediate information, load your daily functions, and import modules.

Keep It Simple
I recommend you keep it simple. Remember, the more calls you make, the slower your shell starting up. That can be frustrating.
Make It Your Own

Some ideas for you to incorporate into your own profile:

  • Load the most recent items from your favourite RSS feeds.
  • Check a status page.
  • Check for any PowerShell module updates.

Have fun!