Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Convert Boolean Values

Posted on April 24, 2012

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

7 thoughts on “Convert Boolean Values”

  1. francisd says:
    April 24, 2012 at 8:26 pm

    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"

    1. Jeffery Hicks says:
      April 24, 2012 at 9:08 pm

      That’s an interesting use case I hadn’t considered.

  2. francisd says:
    April 24, 2012 at 8:37 pm

    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.

  3. Rob Campbell says:
    April 28, 2012 at 3:25 pm

    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…………

    1. Jeffery Hicks says:
      April 28, 2012 at 5:55 pm

      This seems like a reasonable “quick and dirty” approach.

      1. Rob Campbell says:
        April 28, 2012 at 5:59 pm

        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 🙂

      2. Jeffery Hicks says:
        April 28, 2012 at 6:32 pm

        Exactly.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d