2

I want to check whether a powershell object contains any null values. If it does, then it should return a $true

For example

$tags=@{'Artist'='Madonna';`
        'Title'='Like a Prayer';`
        'Genre'=$null; }

How can I test that $tags contains a null value?

2
  • 1
    Well you will need to be more specific, checking if there is a null in a hashtable could be as simple as $tags.ContainsValue($null) but I'll assume you're also looking to check if it contains an empty string and I'll also assume, since you're talking about a powershell obejct, $tags is not actually a hashtable but a pscustomobject instead. Commented Dec 21, 2022 at 21:29
  • Great, that worked. .containsvalue($null) gave me the outcome i required. Thanks. Commented Dec 21, 2022 at 21:38

1 Answer 1

2

You can use .ContainsValue method:

$tags.ContainsValue($null)

Using a containment operator would also work:

$tags.Values -contains $null
$null -in $tags.Values
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.