Last active
September 4, 2017 08:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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