Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active September 4, 2017 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwragg/637d441bf71a7112ae5cdda8968c9513 to your computer and use it in GitHub Desktop.
Save markwragg/637d441bf71a7112ae5cdda8968c9513 to your computer and use it in GitHub Desktop.
PowerShell function to create a partially coloured version of Format-List to highlight defined keywords in one or more colours.
Function Format-ColourList {
Param(
#The input object to be formatted.
[Parameter(Position=0,ValueFromPipeline)]
$InputObject,
#A hashtable of text to colour and the colour of that text. By default colours 'True' as Green and 'False' as Red.
[Hashtable]$Colour = @{True = 'Green'; False = 'Red'}
)
Begin {
Write-Host ''
}
Process {
Write-Host ''
($InputObject | Format-List | Out-String) -split "`n" | ForEach-Object {
If ($_ -match '\w') {
$Output = $false
ForEach ($Entry in $Colour.Keys){
$Text = $_ -split $Entry
If ($Text.count -gt 1) {
Write-Host $Text[0] -NoNewline
Write-Host $Entry -ForegroundColor $Colour.$Entry -NoNewline
Write-Host $Text[-1]
$Output = $true
Break
}
}
If (-not $Output) { Write-Host $_ }
}
}
}
End {
Write-Host ("`n"*2)
}
}
Get-Service | Format-ColourList -Colour @{Stopped = 'Yellow'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment