| by Arround The Web | No comments

How Can I Check if a String is Null or Empty in PowerShell?

PowerShell comprises different data types, including integer, boolean, array, or strings. A string is a combination and sequence of characters used to represent meaningful texts. It normally contains more than one word, but an empty string also exists. However, while working, we sometimes need to check whether the specific string is empty. PowerShell has specific commands that help the users to understand whether the stated string is empty or not.

This post will illustrate a procedure to examine whether the string is null.

How to Check/Examine if a String is Null or Empty in PowerShell?

These are the methods that can be utilized to check whether a string is empty or not:

Method 1: Check if a String is Null or Empty in PowerShell Using IsNullorEmpty() Method

The “IsNullorEmpty()” method is used to examine the provided string and then tell if it is empty or not. It uses the .NET class “system.string” for the corresponding purpose.

Example

For the demonstration, execute the following commands in PowerShell:

> $str = "This is a String"
> [string]::IsNullOrEmpty($str)

According to the given code:

  • First, we have declared a variable having a string value.
  • After that, we used the “[string]::IsNullOrEmpty()” method and passed the defined string as a parameter.
  • The “[string]::IsNullOrEmpty()” approach then checks whether a string is null or empty:

The “False” output confirms that the string is not empty.

Now, let’s check the string when it is empty:

> $str = ""
> [string]::IsNullOrEmpty($str)

In this example code, we assigned an empty string to a variable and then checked the variable using the specified method:

As the string was empty, the IsNullOrEmpty() method returned “True”.

Method 2: Check if a String is Null or Empty in PowerShell Using IsNullOrWhiteSpace()

Another method that is quite similar to the first method is “IsNullOrWhiteSpace()”. It also utilizes the .NET “system.string” class to check whether a certain string is empty or null.

Example

In this example, let’s first check the string which is not empty using the “[string]::IsNullOrWhiteSpace()” method:

> $str1 = "Hello World"
> [string]::IsNullOrWhiteSpace($str1)

Now, let’s check the string which is empty or null in PowerShell:

> $str1 = ""
> [string]::IsNullOrWhiteSpace($str1)

As the string is empty, the IsNullOrWhiteSpace() method returned “True”:

Method 3: Check if a String is Null or Empty in PowerShell Using Conditional If-Else Statement

Final approach to check if the string is null or empty in PowerShell is the usage of the if-else conditional statement.

Example

Let’s test the example in which the string is empty using the if-else statement:

$str=""
if ($str)
{
 Write-Output "String is not empty."
}
else{
 Write-Output "String is empty."
}

In this code example:

  • First, we have created a variable and assigned an empty array to it.
  • After that, we added an if condition and passed the variable as a condition.
  • The condition will be considered as “False” as the string is empty, so the else statement will execute:

That was all checking if a string is null or empty in PowerShell.

Conclusion

The string value can be checked if it is null or empty in PowerShell using multiple methods. These methods include IsNullorEmpty(), IsNullOrWhiteSpace, or conditional if-else statements. If the string is empty, the resultant output will be “True” else, the output result will be “False”. This tutorial has demonstrated several methods to check if the string is null or empty in PowerShell.

Share Button

Source: linuxhint.com

Leave a Reply