Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active September 23, 2021 01:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/31e20c58645b59e42725f0aac0297b6f to your computer and use it in GitHub Desktop.
Save jdhitsolutions/31e20c58645b59e42725f0aac0297b6f to your computer and use it in GitHub Desktop.
PowerShell Candy - my demo files from the PSPowerHour on 26 May, 2020
#requires -version 7.0
$am = @"
_____ __ __ ___ _ __ ______
/ ___/__ ___ ___/ / / |/ /__ _______ (_)__ ___ _ __ / /__ / _/ _/
/ (_ / _ \ _ \ _ / / /|_/ / _ \/ __/ _ \/ / _ \ _ `/ / // / -_) _/ _/
\___/\___\___\_,_/ /_/ /_/\___/_/ /_//_/_/_//_\_, ( ) \___/\__/_//_/
/___/|/
"@
$pm = @"
_____ __ ___ _____ __ _____
/ ___/__ ___ ___/ / / _ | / _/ /____ _______ ___ ___ ___ __ / /__ / _/ _/
/ (_ / _ \ _ \ _ / / __ |/ _/ __/ -_) __/ _ \ _ \ _ \/ _ \_ / // / -_) _/ _/
\___/\___\___\_,_/ /_/ |_/_/ \__/\__/_/ /_//_\___\___/_//_( ) \___/\__/_//_/
|/
"@
$eve = @"
_____ __ ____ _ __ ______
/ ___/__ ___ ___/ / / __/ _____ ___ (_)__ ___ _ __ / /__ / _/ _/
/ (_ / _ \ _ \ _ / / _/| |/ / -_) _ \/ / _ \ _ `/ / // / -_) _/ _/
\___/\___\___\_,_/ /___/|___/\__/_//_/_/_//_\_, ( ) \___/\__/_//_/
/___/|/
"@
$hour = (Get-Date).hour
if ($hour -ge 18) {
"`e[38;5;124m$eve`e[0m"
}
elseif ($hour -ge 12) {
"`e[38;5;227m$pm`e[0m"
}
else {
"`e[38;5;209m$am`e[0m"
}
<# Saved
$am = @"
_____ __ __ ___ _ __ ______
/ ___/__ ___ ___/ / / |/ /__ _______ (_)__ ___ _ __ / /__ / _/ _/__ ______ __
/ (_ / _ \ _ \ _ / / /|_/ / _ \/ __/ _ \/ / _ \ _ `/ / // / -_) _/ _/ -_) __/ // /
\___/\___\___\_,_/ /_/ /_/\___/_/ /_//_/_/_//_\_, ( ) \___/\__/_//_/ \__/_/ \_, /
/___/|/ /___/
"@
$pm = @"
_____ __ ___ _____ __ ______
/ ___/__ ___ ___/ / / _ | / _/ /____ _______ ___ ___ ___ __ / /__ / _/ _/__ ______ __
/ (_ / _ \ _ \ _ / / __ |/ _/ __/ -_) __/ _ \ _ \ _ \/ _ \_ / // / -_) _/ _/ -_) __/ // /
\___/\___\___\_,_/ /_/ |_/_/ \__/\__/_/ /_//_\___\___/_//_( ) \___/\__/_//_/ \__/_/ \_, /
|/ /___/
"@
$eve = @"
_____ __ ____ _ __ ______
/ ___/__ ___ ___/ / / __/ _____ ___ (_)__ ___ _ __ / /__ / _/ _/__ ______ __
/ (_ / _ \ _ \ _ / / _/| |/ / -_) _ \/ / _ \ _ `/ / // / -_) _/ _/ -_) __/ // /
\___/\___\___\_,_/ /___/|___/\__/_//_/_/_//_\_, ( ) \___/\__/_//_/ \__/_/ \_, /
/___/|/ /___/
"@
#>
#requires -version 5.1
#ConvertTo-ASCIIArt.ps1
<#
font list at https://artii.herokuapp.com/fonts_list
font names are case-sensitive
invoke-restmethod https://artii.herokuapp.com/fonts_list
#>
Function ConvertTo-ASCIIArt {
[cmdletbinding()]
[alias("cart")]
[outputtype([System.String])]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a short string of text to convert", ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]$Text,
[Parameter(Position = 1,HelpMessage = "Specify a font from https://artii.herokuapp.com/fonts_list. Font names are case-sensitive")]
[ValidateNotNullOrEmpty()]
[string]$Font = "big"
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $text with font $Font"
$testEncode = [uri]::EscapeDataString($Text)
$url = "http://artii.herokuapp.com/make?text=$testEncode&font=$Font"
Try {
Invoke-Restmethod -Uri $url -DisableKeepAlive -ErrorAction Stop
}
Catch {
Throw $_
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
# Powershell profile prompt designed for PowerShell running in Windows Terminal
#this function uses the Show-Calendar command from the PSCalendar module
#which can be installed from the PowerShell Gallery.
function prompt {
$location = $executionContext.SessionState.Path.CurrentLocation.path
#define some timely emojis
$morning = 0x2615, 0x1F95E, 0x1F953, 0x1F369
$lunch = 0x1F96A, 0x1F355, 0x1F32D
$cup = 0x1F943, 0x1F964
$cocktail = 0x1F378, 0x1F379, 0x1F943, 0x1F377
#What time is it?
$hour = (Get-Date).hour
if ($hour -ge 16) {
$glyph = ConvertTo-Emoji ($cocktail | Get-Random)
}
elseif ($hour -ge 13) {
#afternoon
$glyph = ConvertTo-Emoji ($cup | Get-Random)
}
elseif ($hour -ge 11) {
$glyph = ConvertTo-Emoji ($lunch | Get-Random)
}
else {
#morning
$glyph = ConvertTo-Emoji ($morning | Get-Random)
}
#Some code to shorten long paths in the prompt
#what is the maximum length of the path before you begin truncating?
$len = 33
if ($location.length -gt $len) {
#split on the path delimiter which might be different on non-Windows platforms
$dsc = [system.io.path]::DirectorySeparatorChar
#escape the separator character to treat it as a literal
#filter out any blank entries which might happen if the path ends with the delimiter
$split = $location -split "\$($dsc)" | Where-Object { $_ -match "\S+" }
#reconstruct a shorted path
$here = "{0}$dsc{1}...$dsc{2}" -f $split[0], $split[1], $split[-1]
}
else {
#length is ok so use the current location
$here = $location
}
$dt = Get-Date -Format "MMMM dd hh:mm" #"dd-MM-yy HH:mm:ss"
Write-Host "[$dt]$glyph" -ForegroundColor yellow -NoNewline
#use truncated long path
Write-Host "`e[1mPS$($PSVersionTable.PSversion.major).$($PSVersionTable.PSversion.minor)`e[0m `e[22;38;5;51;48;5;27m$here$('>' * ($nestedPromptLevel + 1))`e[0m" -NoNewLine
Write-Output " "
$left = $host.ui.RawUI.WindowSize.width - 42
#show the calendar in the upper right corner of the console
$pos = [system.management.automation.host.coordinates]::new($left, 0)
Show-Calendar -Position $pos
#rotate headers if running in Windows Terminal
if ($ENV:WT_SESSION) {
#if this is the first time
if (-Not $global:lastChanged) {
$global:lastChanged = Get-Date
}
$min = ((Get-Date) - $global:lastChanged).totalMinutes
#rotate graphics every 11 minutes
if ($min -ge 11) {
C:\scripts\RotateHead.ps1
$global:lastChanged = Get-Date
}
}
} #close function
#requires -version 7.0
<#
PowerShell Candy
PowerShell is about getting work done. But that doesn't mean it has to be drab and gray.
Sometimes it helps to add a little visual pizzazz. Like putting a border around the
heading in a report. Or adding a dash of color to reinforce the data - without
relying on Write-Host! In this session we'll explore the world of ANSI, special
characters and more so that you can start adding a little flair to your PowerShell work.
#>
#some of these demos won't work in the PowerShell ISE
Return "`e[38;5;214mThis is demo file.`e[0m"
#Install-Module PSScriptTools
Import-Module PSScriptTools
#region characters
charmap.exe
[char]0x00B6
[char]0x25CA
#this is from my PSScriptTools module
$PSSpecialChar
#create a line
([string]$PSSpecialChar.Diamond)*20
#endregion
#region ASCII Art
code C:\scripts\ConvertTo-ASCIIArt.ps1
cart "Hello World" computer
cart jeff basic
#endregion
#region emoji
#http://www.unicode.org/emoji/charts/full-emoji-list.html
code C:\scripts\PSEmoji.ps1
ConvertTo-Emoji 0x1F600
Show-Emoji -Start 0x1F600 -Count 25
#endregion
#region ANSI escape
# `e or $([char]0x1b)
# https://en.wikipedia.org/wiki/ANSI_escape_code
# https://leanpub.com/ps7now
Get-PSReadLineOption
code C:\scripts\Update-PSReadline.ps1
"`e[92mIsn't this cool!`e[0m"
#make a bit bolder
"`e[1;92mIsn't this cool!`e[0m"
#reverse
"`e[7;92mIsn't this cool!`e[0m"
#string object
"`e[7;92mIsn't this cool!`e[0m" | Get-Member
#includes the escape code
"`e[7;92mIsn't this cool!`e[0m" | Set-Clipboard
notepad #paste
code C:\scripts\AnsiGreeting.ps1
#endregion
#region My functions
Add-Border "Hello World"
Add-Border "Hello World" -ANSIText "`e[92m"
Add-Border "Hello World" -ANSIText "`e[92m" -Character $PSSpecialChar.WhiteCircle -ANSIBorder "`e[38;5;200m"
Add-Border -textblock (Get-PSWho -AsString) -ANSIBorder "`e[38;5;225m" -Character "="
New-ANSIBar -range (232..255)
New-ANSIBar -Range (40..51) -Gradient -Spacing 3 -Character blacksquare
New-RedGreenGradient
New-RedGreenGradient -Percent .55
code $PSSamplePath\Get-Status.ps1
code $PSSamplePath\ProcessPercent.ps1
#my PS7 prompt when using Windows Terminal
code C:\scripts\ps7terminal-prompt.ps1
#endregion
#These commands work best in a PowerShell session running in Windows Terminal.
# http://www.unicode.org/emoji/charts/full-emoji-list.html
Function ConvertTo-Emoji {
[cmdletbinding()]
[alias("cte")]
Param(
[parameter(Position = 0, Mandatory, ValueFromPipeline, HelpMessage = "Specify a value like 0x1F499 or 128153")]
[int]$Value
)
Process {
if ($env:wt_Session -OR ($host.name -match "studio")) {
[char]::convertfromutf32($Value)
}
else {
Write-Warning 'This command is only supported when running in Windows Terminal at this time.'
}
}
}
Function Show-Emoji {
[cmdletbinding()]
Param(
[parameter(Position = 0, Mandatory,HelpMessage = "Enter the starting Unicode value")]
[int32]$Start,
[Parameter(HelpMessage = "How many items do you want to display?")]
[int]$Count = 20
)
Write-verbose "Starting at $Start and getting $count items"
$counter = 1
Do {
for ($i=1;$i -le 5;$i++) {
write-verbose "Counter = $counter i = $i"
$item = "{0} {1} " -f (ConvertTo-Emoji ($start)),$start
if ($counter -le $count) {
write-host $item -NoNewline
$counter++
$start++
}
}
write-host "`r"
} until ($counter -ge $count)
}
#show-emoji 0x1F600 -Count 20
#rotate the bottom right graphic in a Windows Terminal
[CmdletBinding()]
Param()
#get the path to the WindowsTerminal settings file
$termProfile = (Get-Item -path "$env:localappdata\Packages\Microsoft.WindowsTerminal_*\LocalState\settings.json").FullName
Write-Verbose "Using settings file $TermProfile"
#define an array of images
$pics = (Get-Childitem c:\users\$env:username\onedrive\terminalthumbs).FullName
write-Verbose "Found $($pics.count) image files"
#get the raw json contents
$raw = Get-Content -path $termProfile -Raw
Write-Verbose "Creating a PowerShell object from the raw data"
$json = Convertfrom-json -InputObject $raw
#find the current background image in the specified profile
#$current = ($json.profiles.list).where( { $_.guid -eq '{18b5ce4a-c242-46f0-980a-ffd888901802}' }).backgroundImage.replace("\", "\\")
$current = ($json.profiles.list).where( { $_.name -eq 'PowerShell 7' }).backgroundImage.replace("\", "\\")
Write-Verbose "Current image is $Current"
if ($current) {
#get a random new graphic
$new = ($pics | Get-Random).replace("\", "\\")
Write-Verbose "Replacing with $new"
#replace the old file with the new.
#There is an assumption that you aren't using the same image in multiple profiles
Write-Verbose "Replacing json data"
$raw.Replace($current,$new) | Set-Content -Path $termProfile
}
else {
Write-Warning "No current background image found"
}
Write-Verbose "RotateHead complete."
#Update some of the PSReadline color settings to make them easier to read
# https://en.wikipedia.org/wiki/ANSI_escape_code
if ($IsCoreCLR) {
$esc = "`e"
}
else {
$esc = $([char]0x1b)
}
Set-PSReadLineOption -Colors @{
Parameter = "$esc[96m"
Operator = "$esc[38;5;47m"
Comment = "$esc[92m"
String = "$esc[38;5;51m"
}
#make error messages green
$host.PrivateData.ErrorForegroundColor = "green"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment