In this post, I will show you how to verify if a string is empty, null or having white spaces using Powershell.
Checking if a string is NULL or EMPTY is very common requirement in Powershell script. If we don’t do that we will end up with run time errors if we try to perform some operation on that string variable which is empty or null. So the question now is, how to check it?
Well, below is the most often used technique to check if a string is NULL or empty
if($mystring) { Write-Host "string is not empty" } else { Write-Host "String is EMPTY or NULL" }
Most scripts use this method, however we can make things better by using “System.String” dotnet class. It has a method IsNullOrEmpty() which returns true if the passed string is null or empty. See the below example for clarity
IF([string]::IsNullOrEmpty($mystring)) { Write-Host "Given string is NULL or EMPTY" } else { Write-Host "Given string has a value" }
Both the methods described above gives you the same functionality. But if you want more meaningful and neat look to your code, I would prefer the second method.
Now you might get a question, what if the $mystring has white space as value. It is neither a EMPTY value nor a NULL value so we can not use IsNullOrEmpty() method in this case. If you try it, it will return False which means string is not NULL or EMPTY. In such cases we can use another method available with System.String class. That is IsNullOrWhiteSpace() . The good thing about this method is it covers all three cases, NULL, EMPTY, and WHITESPACE. It will work for any number whitespaces in the variable. See the below examples for clarity
IF([string]::IsNullOrWhiteSpace($string1)) { Write-Host "Given string is NULL or having WHITESPACE" } else { Write-Host "Given string has a value" } $string1 = " " IF([string]::IsNullOrWhiteSpace($string1)) { Write-Host "Given string is NULL or having WHITESPACE" } else { Write-Host "Given string has a value" } $string1 = "" IF([string]::IsNullOrWhiteSpace($string1)) { Write-Host "Given string is NULL or having WHITESPACE" } else { Write-Host "Given string has a value" }
After executing above code you will get Given string is NULL or having WHITESPACE three times in output. So, it is clear that theIsNullOrWhiteSpace()method is working for detecting NULL, EMPTY and WHILESPACE strings.
Hope this helps and happy learning..
Please feel free to write in comments section if you have any questions.
Comments on this entry are closed.
Thanks, it was really helpful.
I get
Method invocation failed because [System.String] doesn’t contain a method named ‘IsNullOrWhiteSpace’.
Windows server 2008r3
.NET Framework
Supported in: 4.5, 4
.NET Framework Client Profile
Supported in: 4
Portable Class Library
Supported in: Portable Class Library
.NET for Windows Store apps
Supported in: Windows 8
Found this very helpful, thank you.
This was not helpful at all but thank you.
@PSGuru, Is your requirement different or script is not doing what is claims??
Extremely helpful, I’ve been tasked with importing XML files into Powershell for mainframe job setups, and often we need to determine if there is nothing in a specific XML field, but it doesn’t show up as “$null” or “$empty” – if ([string]::IsNullOrWhiteSpace($variable)) saved me!
Very VERY helpful. The only $NULL checks I’ve ever had to do worked fine simply by calling $NULL, until working with Active Directory attributes. Thanks a million!
Helpful in the way of using String.
However, the initial statement that
if ($myString)
works the same, and is good for checking for null values IS WRONG!
Consider:
$myString=’false’ => “string is not empty” — ok.
$myString=$false => “String is EMPTY or NULL” — wrong!
$myString=0 => “String is EMPTY or NULL” — wrong!
$myString=@(0) => “String is EMPTY or NULL” — wrong!