You guys are making this too hard. PowerShell handles this quite elegantly e.g.:
Copy> $str1 = $null
> if ($str1) { 'not empty' } else { 'empty' }
empty
> $str2 = ''
> if ($str2) { 'not empty' } else { 'empty' }
empty
> $str3 = ' '
> if ($str3) { 'not empty' } else { 'empty' }
not empty
> $str4 = 'asdf'
> if ($str4) { 'not empty' } else { 'empty' }
not empty
> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
one or both empty
> if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
neither empty
Answer from Keith Hill on Stack Overflow Top answer 1 of 16
916
You guys are making this too hard. PowerShell handles this quite elegantly e.g.:
Copy> $str1 = $null
> if ($str1) { 'not empty' } else { 'empty' }
empty
> $str2 = ''
> if ($str2) { 'not empty' } else { 'empty' }
empty
> $str3 = ' '
> if ($str3) { 'not empty' } else { 'empty' }
not empty
> $str4 = 'asdf'
> if ($str4) { 'not empty' } else { 'empty' }
not empty
> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
one or both empty
> if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
neither empty
2 of 16
719
You can use the IsNullOrEmpty static method:
Copy[string]::IsNullOrEmpty(...)
Powershell command - null or empty comand is not working
case1 if(($RequestorEmail -ne $null) -or ($RequestorEmail -ne "")) case2 if ( -not [string]::IsNullOrEmpty( $RequestorEmail ) ) Windows for business | Windows Server | User experience | PowerShell · Windows for business | Windows Server | User experience | PowerShell ... In the situation you present, $RequestorEmail contains an empty ... More on learn.microsoft.com
Switch test for $null and empty string
Check this box if they match or the issue you are reporting is not version specific. https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch · I have not confirmed other versions, but assume this has always been the case. ... Notice that the empty string value doesn't match $null ... More on github.com
Best way to check for a null variable?
$null -ne $value If you are explicitly checking for null, this is what you want. Having null on the left catches some edge cases. If you just check on the variable, you are really checking if it is not true. Lots of values besides null match that. Everything you ever wanted to know about null More on reddit.com
Passing $null to a [string] parameter unconditionally causes conversion to [string]::Empty
Passing $null to any of these functions outputs $null. The only parameter type I haven't found a way to which to pass $null without conversion is [string]; PowerShell always converts $null to [string]::Empty. More on github.com
SID-500.COM
sid-500.com › 2021 › 07 › 07 › powershell-null-vs-isnullorempty
PowerShell: $null vs ::IsNullOrEmpty – SID-500.COM
July 7, 2021 - It’s just an empty string. Which brings me to $justempty. What will happen here? Oh, right, $justempty is really empty, so it is $null. Simply put, it is just nothing. Now we explore the ::IsNullOrEmpty statement. It’s a .NET statement to figure out, if a value is null or empty. Not surprisingly, both value show true, because the first one is empty and the second one is just nothing, so it is $null. Nice. Mission accomplished. Keep on working with PowerShell!
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › deep-dives › everything-about-null
Everything you wanted to know about $null - PowerShell | Microsoft Learn
June 11, 2024 - PS> function Get-Nothing {} PS> $value = Get-Nothing PS> $null -eq $value True · $null values impact your code differently depending on where they show up. If you use $null in a string, then it's a blank value (or empty string).
PowerShell Test-Path
powershellfaqs.com › powershell-isnullorempty
PowerShell IsNullOrEmpty() Example
July 29, 2024 - Null: Represents the absence of a value. In PowerShell, null is represented by $null. Empty: Refers to a string with no characters, represented by "" or [string]::Empty.
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 513246 › powershell-command-null-or-empty-comand-is-not-wor
Powershell command - null or empty comand is not working - Microsoft Q&A
August 13, 2021 - case1 if(($RequestorEmail -ne $null) -or ($RequestorEmail -ne "")) case2 if ( -not [string]::IsNullOrEmpty( $RequestorEmail ) ) Windows for business | Windows Server | User experience | PowerShell · Windows for business | Windows Server | User experience | PowerShell ... In the situation you present, $RequestorEmail contains an empty string.
miajimyu note
miajimyu.com › en › docs › powershell › powershell-tips › how-to-judge-null-and-empty
How to check for null and empty characters in variables in PowerShell | miajimyu note
August 9, 2020 - # Example 1 PS> $var = $null PS> ... # Example 3 PS> $var = "a" PS> if($var){"not null or empty"}else{"null or empty"} not null or empty · Use [String]::IsNullOrEmpty()....
GitHub
github.com › MicrosoftDocs › PowerShell-Docs › issues › 9255
Switch test for $null and empty string · Issue #9255 · MicrosoftDocs/PowerShell-Docs
October 2, 2022 - $values = '', 5, $null switch ( $values ) { '' { "Value '$_' is an empty string" ; continue } $null { "Value '$_' is `$null or an empty string" } default { "Value [$_] is not an empty string or `$null" } }
Author MicrosoftDocs
HeelpBook
heelpbook.net › 2019 › powershell-variable-for-null-empty-string-and-white-space
Powershell – Variable for Null, Empty String and White Space
April 2, 2019 - Our variable may truly be absent ... for which we do not set a value. If our variable is a string, it could contain a value of “”, which is an empty value as far as a string is concerned, but is still a value when compared to $null....
Reddit
reddit.com › r/powershell › best way to check for a null variable?
r/PowerShell on Reddit: Best way to check for a null variable?
August 10, 2019 -
If(!$Variable) vs if($Variable -eq $null)
Is there much difference or clear advantages between the two?
Top answer 1 of 11
18
$null -ne $value If you are explicitly checking for null, this is what you want. Having null on the left catches some edge cases. If you just check on the variable, you are really checking if it is not true. Lots of values besides null match that. Everything you ever wanted to know about null
2 of 11
15
It really depends on what you are trying to do. if(!$Variable) { # code } Will evaluate to true if the value of $Variable is and empty string (0 length string), 0, an empty array, an empty hash, or any object where implicit casting to Boolean is implemented and the object’s value evaluates to False., so while both examples will function identically when your variable is Null, the second method you outline will evaluate to true ONLY when your variable is null. As far as which is better, my opinion is that it is about your coding style and code readability rather than any optimization concerns.
GitHub
github.com › PowerShell › PowerShell › issues › 4616
Passing $null to a [string] parameter unconditionally causes conversion to [string]::Empty · Issue #4616 · PowerShell/PowerShell
August 19, 2017 - Passing $null to any of these functions outputs $null. The only parameter type I haven't found a way to which to pass $null without conversion is [string]; PowerShell always converts $null to [string]::Empty.
Author PowerShell