In PowerShell IsNullOrEmpty
built-in static method of the .Net Framework System.String
class can be used to determine if a string variable is null or empty.
The IsNullOrEmpty
method functionality and combining it with other validation methods, you can write error-free PowerShell scripts.
The IsNullOrWhiteSpace
built-in static method of the .Net Framework System.String
class can be used efficiently to determine if a string variable contains only null or whitespace characters.
In this article, we will discuss how to use the PowerShell isNullOrEmpty method to check if the string is empty or null and the IsNullOrWhiteSpace method to check if a string contains null or whitespace characters.
Check If a String is Null or Empty
Use the IsNullOrEmpty method to check if a string variable is null or empty, follow this syntax:
$tempVariable if([string]::IsNullOrEmpty($tempVariable)) { Write-Host "The string is null or empty." } else { Write-Host "The strign is not null or empty." }
In the above PowerShell script, we have defined a variable $tempVariable
. To check if a variable is null or empty, the if statement uses the static method IsNullOrEmpty
to take the variable as input and evaluates if a string variable is null or empty.
The output of the above PowerShell script determines variable has any value or is null and prints the output using the Write-Host
cmdlet.
PS C:\> $tempVariable
PS C:\> if([string]::IsNullOrEmpty($tempVariable))
{
Write-Host "The string is null or empty"
}
else
{ Write-Host "asd"
}
The string is null or empty
PS C:\>
Let’s take a few more practical examples of IsNullOrEmpty in PowerShell.
PowerShell Variable Null or Empty Check for User Input
Let’s take an example to validate whether the user input is null or empty using the do-while loop, the following syntax:
do { $username = Read-Host "Please enter a user name" } while ([string]::IsNullOrEmpty($username))
In the above PowerShell script, we have used a do-while loop
to read the input from the user about the user name and determine if the variable is null or empty. If the variable is null or empty, it will again ask the user to “Please enter a user name”.
The output of the above PowerShell script to check if a variable is null or empty is:
PowerShell If Variable is Null or Empty in Function
To ensure mandatory parameters in functions and check if a variable is not empty or null, use the following syntax:
function Validate-String { param ( [string]$inputString ) if ([string]::IsNullOrEmpty($inputString)) { Write-Error "The input string parameter cannot be null or empty" return } # Continue processing the input string parameter }
In the above PowerShell script, we have created a function Validate-String. This function takes the input parameter $inputString
. In the function, it checks if the variable is empty or null using the IsNullOrEmpty
static method System.String
class.
The output of the above PowerShell script validates whether the variable is null or empty in the function.
PS C:\> Validate-String -inputString ""
Validate-String : The input string parameter cannot be null or empty
At line:1 char:1
+ Validate-String -inputString ""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Validate-String
Check If a String is Null or WhiteSpace Characters
To check if a string contains only null or whitespace characters, use the IsNullOrWhiteSpace method, and follow the syntax:
$userInput = "shelladmin" if ([string]::IsNullOrWhiteSpace($userInput )) { Write-Host "The string is null or contains only whitespace characters" } else { Write-Host "The string does not contains null or whitespace characters" }
In the above PowerShell script, the if statement uses the IsNullOrWhiteSpace method to identify if a string variable contains null or only whitespace characters and print the output.
The above PowerShell script validates the string variable and as it is not null or contains whitespace characters, it returns the output as:
The string does not contains null or whitespace characters
Common Mistakes and Best Practices
There is a difference between null and empty strings, hence use the correct method to validate the input. Use -eq $null
or -ne $null
to check null values in non-string data types that can be nullable.
Use IsNullOrWhileSpace
and IsNullOrEmpty
methods to validate string variables based on the requirement.
Conclusion
I hope the above article on how to use the PowerShell IsNullOrEmpty method to identify null or empty strings is helpful to you.
Use the IsNullOrWhiteSpace() method to validate if a string contains null or whitespace characters.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.