Formatting
Color formatting
Adding color to your output
write-host -BackgroundColor DarkYellow -ForegroundColor Black "Bumblebee"
Short hand version
write-host -b DarkYellow -f Black "Bumblebee"
String formatting
Writing a variable the traditional way
$users = @('Bob','Alice','Susan')
write-host "Greetings from $users[0] to $users[1]"
Writing a variable with formatting and escape signs
$users = @('Bob','Alice','Susan')
write-host ("Greetings from {0} to {1} " -f $users[0],$users[1])
write-host ("Greetings from {1} to {0} " -f $users[0],$users[1])
write-host ("Greetings from {0,15} to {1,15} " -f $users[0],$users[1])
write-host ("Greetings from {0} `n`t to {1} " -f $users[0],$users[1])
Writing a variable with formatting
function PrettyPrint-Array {
param(
[string[]]$array = @()
)
write-host ("| {0,15} |" -f "Names")
(0..18) | % { write-host -nonewline "#" }
write-host ""
foreach ($element in $array) {
write-host ("| {0,15} |" -f $element)
}
}
$users = @('Bob','Alice','Susan')
PrettyPrint-Array -array $users
Writing other types
write-host ("{0,-10} | {1,10}" -f "left","right")
write-host ("{0}" -f 3.1456)
write-host ("{0,10}" -f 3.1456) # threat it like a string
write-host ("{0:D10}" -f 3) # leading zero
write-host ("{0,15:D10}" -f 3) # leading zero and max size
write-host ("{0:N10}" -f 3.1456) # floats / numbers
write-host ("{0,15:N10}" -f 3.1456) # floats / numbers
write-host ("{0:X}" -f 14) # hexdecimal
write-host ("{0:C10}" -f 3.1456) # currency
write-host ("{0:D}" -f (get-date)) #
write-host ("{0:u}" -f (get-date)) # sortable
write-host ("{0,30:yy MM dd hh mm ss}" -f (get-date)) # sortable
write-host (get-date).ToString("yy MM dd hh mm ss") #custom
write-host ("i want my brackets {{ {0} }}" -f (get-date))
Dot net related format.aspx#Format_Custom) Number Dates Composite
function Write-PingPong {
param(
[int]$height=20,
[int]$width=60,
[int]$left = 2,
[int]$right = 2,
[int]$ballx = 3,
[int]$bally = 5
)
if ($left -lt 1) { $left = 1}
if ($left -gt ($height-1)) { $left = $height-1}
if ($right -lt 1) { $right = 1}
if ($right -gt ($height-1)) { $right = $height-1}
[int[]]$lefts = @(($left-1),$left,($left+1))
[int[]]$rights = @(($right-1),$right,($right+1))
$lines = @()
$ln = ("+{0,$width}+" -f "").ToCharArray()
foreach($i in (1..($width-1))) {
$ln[$i] = "-"
}
$lines += ($ln -join "")
$ln = ("|{0,$width}|" -f "")
foreach($i in (0..$height)) {
$l = $ln.ToCharArray()
if ($i -in $lefts) {
$l[1] = '#'
}
if ($i -in $rights) {
$l[$width-1] = '#'
}
if ($i -in $bally) {
$l[$ballx] = '*'
}
$lines += ($l -join "")
}
$ln = ("+{0,$width}+" -f "").ToCharArray()
foreach($i in (1..($width-1))) {
$ln[$i] = "-"
}
$lines += ($ln -join "")
$joined = ($lines -join "`n")
clear-host
write-host $joined
}
function Play-PingPong {
param(
$dirx = +1,
$diry = +1,
$stepf = 1,
$height = 20,
$width = 60,
$fps = 5,
$playforstep = 500
)
[int]$msbetweenframes = 1000/$fps
[int]$ballx = ($width/2)
[int]$bally = Get-Random -Minimum 0 -Maximum $height
foreach ($runtime in (0..$playforstep)) {
$skew = $( measure-command {
$ballx = $ballx + ($dirx*$stepf)
$bally = $bally + ($diry*$stepf)
if ($ballx -ge ($width-1) -or $ballx -le 1 ) {
$dirx = $dirx*-1
$ballx = $ballx + ($dirx*$stepf*2)
}
if ($bally -ge $height -or $bally -le 0 ) {
$diry = $diry*-1
$bally = $bally + ($diry*$stepf*2)
}
write-pingpong -left $bally -right $bally -ballx $ballx -bally $bally -height $height -width $width
}).milliseconds
if ($skew -lt $msbetweenframes) {
start-sleep -milliseconds ($msbetweenframes-$skew)
}
}
}