none
PowerShell IsPresent incorrectly reporting false for explicitly specified switch parameter RRS feed

  • Question

  • Why are PowerShell versions 3.0 and 4.0 unreliable when it comes to reporting the correct IsPresent status of a switch parameter?

    Here's an example that illustrates the problem that IsPresent is reported as False when clearly it is present:

    function TestMe {
    param(
    [Parameter(Mandatory=$false)]
    [switch] $Hidden,
    [Parameter(Mandatory=$false)]
    [bool] $Hide=$false
    )
    Write-Verbose 'START----------------------'
    Write-Verbose ('Hidden.IsPresent={0}' -f $Hidden.IsPresent)
    Write-Verbose ('Hidden={0}' -f $Hidden.ToBool())
    Write-Verbose ('Hide={0}' -f $Hide)
    Write-Verbose 'END----------------------'
    }
    Clear-Host
    TestMe -Hidden -Verbose
    # Why does the following indicate IsPresent=false when clearly it should indicate IsPresent=true ?
    TestMe -Hidden:$False -Verbose
    TestMe -Verbose
    TestMe -Hide $true -Verbose

    The output from the above is as follows.  Pay close attention to the PowerShell bug shown in the output from the second call to TestMe:

    VERBOSE: START----------------------
    VERBOSE: Hidden.IsPresent=True
    VERBOSE: Hidden=True
    VERBOSE: Hide=False
    VERBOSE: END----------------------
    VERBOSE: START----------------------
    VERBOSE: Hidden.IsPresent=False <--- THIS IS THE BUG IN POWERSHELL 3.0 and 4.0
    VERBOSE: Hidden=False
    VERBOSE: Hide=False
    VERBOSE: END----------------------
    VERBOSE: START----------------------
    VERBOSE: Hidden.IsPresent=False  
    VERBOSE: Hidden=False
    VERBOSE: Hide=False
    VERBOSE: END----------------------
    VERBOSE: START----------------------
    VERBOSE: Hidden.IsPresent=False
    VERBOSE: Hidden=False
    VERBOSE: Hide=True
    VERBOSE: END----------------------



    If you think development is tough now, think how hard it was using punched cards :-)


    Wednesday, July 30, 2014 12:51 PM

All replies

  • Start simple and analyze incrementally:

    PS C:\scripts> function TestMe {
    >>      param(
    >>          [switch]$Hidden,
    >>          [bool]$Hide
    >>      )
    >>      $PSBoundParameters
    >> }
    >> TestMe
    >>
    PS C:\scripts> TestMe -hidden
    
    Key                                                         Value
    ---                                                         -----
    Hidden                                                      True
    
    
    PS C:\scripts> TestMe -hide $true
    
    Key                                                                                                               Value
    ---                                                                                                               -----
    Hide                                                                                                               True
    
    
    PS C:\scripts> TestMe -Hidden -hide $true
    
    Key                                                         Value
    ---                                                         -----
    Hidden                                                      True
    Hide                                                        True
    
    
    PS C:\scripts>
    
    
    
    
    


    ¯\_(ツ)_/¯

    Wednesday, July 30, 2014 12:59 PM
  • The difference between jrv's example and the op's example is that in the op's example parameters are initialized with the default values, and in the jrv's example they are not. So no explanation. I'm trying to figure out the same problem.
    Thursday, January 31, 2019 12:12 AM