First, let me state right off the bat that what I have here should be for very special use cases and is NOT something I feel you need to be using at all. Now the use case: You are preparing a report of some sort for a non-technical user and instead of displaying True or False, you need to show Yes or No. Or depending on the object and property some other alternate text; maybe "Working"/"NotWorking" or "Green"/"Red". I get it. Sometimes you need brain dead output.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Normally, a boolean value and its property should be self sufficient.
PS C:\> get-process | Select ID,Name,Responding -first 5
Id Name Responding
-- ---- ----------
3344 cAudioFilterAgent64 True
3448 cfp True
5384 CFSwMgr True
1576 chrome True
2708 chrome True
The Responding property is a Boolean. One way to alter this output is to use a simple hashtable with Select-Object.
PS C:\> get-process | Select ID,Name,@{Name="ProcessIsOK";Expression={
>> if ($_.Responding) {"Yes"} else {"No"}}} -first 5
>>
Id Name ProcessIsOK
-- ---- -----------
3344 cAudioFilterAgent64 Yes
3448 cfp Yes
5384 CFSwMgr Yes
1576 chrome Yes
2708 chrome Yes
Let me re-iterate that this type of code should be an EXCEPTION. I would hope that 99% of the time you are working with objects and not worrying about parsing text. That said, I threw together a simple function that allows you to substitute your values for $True and $False.
Function Convert-Boolean {
Param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
[boolean]$Boolean,
[array]$Alternate=("Yes","No")
)
Begin {
$T=$Alternate[0]
$F=$Alternate[1]
}
Process {
if ($Boolean) {
Write $T
}
else {
Write $F
}
}
End {#not used
}
} #function
The function takes a Boolean value and writes an alternate text string. The default is Yes/No. Using this function, here's my revised example:
PS C:\> get-process | Select ID,Name,@{Name="ProcessIsOK";Expression={
>> $_.Responding | Convert-Boolean }} -first 5
>>
Id Name ProcessIsOK
-- ---- -----------
3344 cAudioFilterAgent64 Yes
3448 cfp Yes
5384 CFSwMgr Yes
1576 chrome Yes
2708 chrome Yes
Or using an alternate string:
PS C:\> get-process | Select ID,Name,@{Name="ProcessIsOK";Expression={
>> $_.Responding | Convert-Boolean -Alternate "OK","NotOK"}} -first 5
>>
Id Name ProcessIsOK
-- ---- -----------
3344 cAudioFilterAgent64 OK
3448 cfp OK
5384 CFSwMgr OK
1576 chrome OK
2708 chrome OK
The alternate values are passed as an array.
Again, (can you tell how I feel about this?), this type of PowerShell command should be used in the rare situations where you need brain-dead output. Here's an example of what NOT to do:
PS C:\> get-process | Select ID,Name,@{Name="ProcessIsOK";Expression={
>> $_.Responding | Convert-Boolean -Alternate "OK","NotOK"}} | Where {$_.ProcessIsOK -eq "NotOK"}
Bad, bad, bad.
All of that said, if you would like to try this out, or perhaps you actually need it, you can download Convert-Boolean and give it a go.
I would actually use a function like this when I need a shortcut for if/else or switch when dealing with boolean variables. For example, if I want to call “cmd /c dir” with some optional parameters (system, hidden, etc) I can do something like this:
$d = $directory| Convert-Boolean -Alternate 'D', '-D'
$h = $hidden | Convert-Boolean -Alternate 'H', '-H'
$s = $system | Convert-Boolean -Alternate 'S', '-S'
$r = $readOnly | Convert-Boolean -Alternate 'R', '-R'
$rec = $recurse| Convert-Boolean -Alternate '/s', ''
cmd /c "dir /a$d$h$s$r $rec"
That’s an interesting use case I hadn’t considered.
Well, it would actually be a shortcut if I also had to deal with nulls, but as it is, maybe it’s best described as a less “bracy” version of if/else or switch. I also find it very readable and very easy to edit.
Alternatively,
function true {‘Yes’}
function false {‘No’}
get-process | Select ID,Name,@{Name=”ProcessIsOK”;Expression={&($_.Responding)}} -first 5
There’s probably something horribly wrong with that…………
This seems like a reasonable “quick and dirty” approach.
That’s usually the kind of solution that gets applied to those “very special cases” where you’re having to do something you probably shouldn’t even be doing at all 🙂
Exactly.