Bugs Bunny
Bugs Bunny

Reputation: 385

Powershell get all varibles that equals false

I got a script that checks for a file and if found sets the variable $true, in the end of the script I'm trying to print what variables equal false and I can't make it work I tried the code below but it doesn't work

Get-Variable | Where-Object Value -like $false

Upvotes: 0

Views: 154

Answers (1)

vonPryz
vonPryz

Reputation: 24071

Pass Get-Variable's results to where-object and check for value and type. If the check omits type, results are a bit surprising. Like so,

$foo = $true
$bar = $false

Get-Variable | ? { $_.value -eq $false  }

Name                           Value
----                           -----
bar                            False
DebugPreference                SilentlyContinue
false                          False
InformationPreference          SilentlyContinue
NestedPromptLevel              0
VerbosePreference              SilentlyContinue
WhatIfPreference               False

# Add type check too
Get-Variable | ? { $_.value -eq $false -and $_.value -is [boolean] }

Name                           Value
----                           -----
bar                            False
false                          False
WhatIfPreference               False

Upvotes: 1

Related Questions