You guys are making this too hard. PowerShell handles this quite elegantly e.g.:
> $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.:
> $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:
[string]::IsNullOrEmpty(...)
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
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....
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