Skip to content

Instantly share code, notes, and snippets.

@Smalls1652
Last active September 1, 2020 15:53
Show Gist options
  • Save Smalls1652/a5fda7004dc2d5471d2f088aa4cd2e16 to your computer and use it in GitHub Desktop.
Save Smalls1652/a5fda7004dc2d5471d2f088aa4cd2e16 to your computer and use it in GitHub Desktop.
My PowerShell Profiles

My PowerShell Profiles

My custom PowerShell profile

These are my personal PowerShell profiles with a customized prompt. The primary style of my prompt was to have a visual indicator as to what version of PowerShell I am running. I tend to work in PowerShell 7.0 more than PowerShell 5.1; however, due to some compatability issues with 7.0, I have to flip-flop between the two. Most of my compatability concerns lie with some Microsoft modules not supporting .NET Core yet (Crazy, right?) or if I have to do something that can only be done in 5.1.

On Windows, I even have it change the > to a $ when running in an administrator context.

PowerShell prompt in admin context

Installation of the profile

Prerequisites

If using PowerShell 7.0, you need to ensure that a PowerShell directory exists for your specific platform. Run these commands for your specific platform if you're not sure:

Windows:

if (-not (Test-Path -Path "~\Documents\PowerShell\")) { New-Item -Path "~\Documents\PowerShell\" -Type "Directory" }

macOS/Linux:

if (-not (Test-Path -Path "~\.config\powershell\")) { New-Item -Path "~\.config\powershell\" -Type "Directory" }

Install

Get the RAW URL for the profile you want and then run this command:

# Replace "/Url/goes/here" with the RAW URL of the profile file.
Invoke-WebRequest -Uri "/Url/goes/here" -OutFile $PROFILE

Updates

2020.09.00

  • Changed the handling of the location path if the current directory structure is the user's profile. Instead of showing the whole path, it will show ~ as the location.
    • For example if the user's profile is C:\Users\jdoe123 and the current directory is C:\Users\jdoe123\Downloads, it will show up as ~\Downloads in the prompt.
<#
# Smalls_Microsoft.PowerShell.7.0.0_Profile
## Platform: macOS/Linux
## PowerShell Version: 7.0
## Version: 2020.05.00
#>
function Prompt {
$color = @{
Reset = "`e[0m"
Red = "`e[31;1m"
Green = "`e[32;1m"
Yellow = "`e[33;1m"
Grey = "`e[37;0m"
White = "`e[37;1m"
Invert = "`e[7m"
RedBackground = "`e[41m"
}
$CurrentDir = (Get-Location).Path
switch ($CurrentDir -like "$($env:USERPROFILE)*") {
$true {
$CurrentDir = $CurrentDir.Replace($env:USERPROFILE, "~")
break
}
Default {
break
}
}
$PSVersion = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
$promptKey = ">"
$promptText = "$($color.Reset)(PS $($PSVersion)) [$($color.Green)$($CurrentDir)$($color.Reset)]$($promptKey)$($color.Reset)"
"${promptText} "
}
#Set the edit mode to 'Windows'
Set-PSReadLineOption -EditMode "Windows"
#Add 'ctrl+k' as a key combo for activating 'MenuComplete'
Set-PSReadLineKeyHandler -Function "MenuComplete" -Chord "Ctrl+k"
<#
# Smalls_Microsoft.PowerShell.5.1.0_Profile
## Platform: Windows
## Version: 2020.09.00
#>
function Prompt {
$CurrentDir = (Get-Location).Path
switch ($CurrentDir -like "$($env:USERPROFILE)*") {
$true {
$CurrentDir = $CurrentDir.Replace($env:USERPROFILE, "~")
break
}
Default {
break
}
}
$PSVersion = "(PS $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor))"
Write-Host $PSVersion -NoNewline
Write-Host " " -NoNewline
Write-Host "[" -NoNewline
Write-Host $CurrentDir -ForegroundColor Green -NoNewline
Write-Host "]" -NoNewline
$promptKey = $null
switch ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$true {
$promptKey = "`$ "
break
}
Default {
$promptKey = "> "
break
}
}
return $promptKey
}
<#
# Smalls_Microsoft.PowerShell.7.0.0_Profile
## Platform: Windows
## Version: 2020.09.00
#>
function Prompt {
$color = @{
Reset = "`e[0m"
Red = "`e[31;1m"
Green = "`e[32;1m"
Yellow = "`e[33;1m"
Grey = "`e[37;0m"
White = "`e[37;1m"
Invert = "`e[7m"
RedBackground = "`e[41m"
}
$CurrentDir = (Get-Location).Path
switch ($CurrentDir -like "$($env:USERPROFILE)*") {
$true {
$CurrentDir = $CurrentDir.Replace($env:USERPROFILE, "~")
break
}
Default {
break
}
}
$PSVersion = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
$promptKey = $null
switch ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$true {
$promptKey = "$($color.Red)`$"
break
}
Default {
$promptKey = ">"
break
}
}
$promptText = "$($color.Reset)(PS $($PSVersion)) [$($color.Green)$($CurrentDir)$($color.Reset)]$($promptKey)$($color.Reset)"
"${promptText} "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment