Check if String is Null or Empty in PowerShell

Check if String is null or empty in PowerShell

Using -not and -eq Operators

Use the -not operator to check if the string is null or empty in PowerShell.

First, we declared the $string variable and initialized it with $null. What is $null? It is an automatic variable in PowerShell representing the NULL value. Now, what is NULL? We can think of it as an empty or unknown value. Any variable would be Null until we assign any object or value to it.

Next, we used the if statement with -not and -eq operators to check if the $string is null or empty. Here, -not is a logical not operator used to negate the outcome of the expression that follows it. So, in the above code snippet, the -not negated the result of $string; since the $string is null, which is a False value in PowerShell, the -not will turn it into True.

The -eq operator, which represents equal to. It was used to compare the two values to determine whether they were equal. Next, we compared the $string with an empty string represented with ''. If conditions are satisfied, the if statement will be executed; otherwise, the else block will be.

Here, we used logical or represented with -or, which will help execute the if block if $string -eq '' or -not $string was satisfied. Finally, we used Write-Host to display custom messages on the host, our PowerShell console. Similarly, we can use -eq operator as follows.

Use the -eq operator to check if the string is null or empty in PowerShell.

This code is similar to the previous example, but we used the -eq operator to compare the two values to determine whether they are equal. We compared $string with an empty string represented with '' and $null. If conditions are satisfied, the if statement will be executed; otherwise, the else block will be.

Using IsNullOrEmpty() Method

Use the IsNullOrEmpty() method to check if the string is null or empty in PowerShell.

For the above code snippet, we used IsNullOrEmpty() method of the string class to determine if the $string is empty or null. This method took one parameter of the string type and returned a Boolean value based on the given $string. The outcome would be True if the $string is null or empty string, which will result in executing the if block; otherwise, it would be False, and the else block would be executed.

You can use the following solution if you also want to include whitespaces while testing the variable.

Use the IsNullOrEmpty() and IsNullOrWhitespace() methods to check if the string is null or empty in PowerShell.

In the above code fence, IsNullOrEmpty() checked if $string is empty or null while IsNullOrWhitespace() finds if the $string is null or having whitespaces. If any of the two methods return True, the if block will run; otherwise, the else block.

Using -notmatch Operator with Regex

Use the -notmatch operator with a regular expression to check if the specified string is null or empty in PowerShell.

Here, we used the -notmatch operator with a regular expression \S to check if the $string is null or empty. Here, \S means any non-whitespace character. If the $string contains any non-whitespace character, the -notmatch operator will return a False and else block will be executed; otherwise, Write-Host will run inside the if block.

Using Type Casting

Use type casting, also known as dynamic casting to check if the given string is null or empty in PowerShell.

Here, we cast the $string to the bool type at runtime to get Boolean values to know if the $string variable is empty or null. The above code will display False if $string is empty or null; otherwise, True.

That’s all about how to check if String is null or Empty in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *