Dude, a String Is a String in PowerShell

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about using string methods to determine null or empty in Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! I have a problem with a script. It is used to write data to another application, but sometimes the variables in it are empty. I have tried to detect this by checking to see if the variable is equal to $null, but this does not work very well. I need a different way to do this. Can you help?

—JB

Hey, Scripting Guy! Answer Hello JB,

Microsoft Scripting Guy, Ed Wilson, is here. When I was looking out the window this morning, I saw a squirrel drop from a tree, look up, and then bunny hop across the yard until he jumped onto another tree and disappeared. I know it was a squirrel because I saw the big bushy tail. But if the Scripting Wife had told me she saw a small furry animal hopping across the yard, I would have assumed it was a rabbit.

Put another way, I can check the weather app on my Windows 10 laptop to see if it is hot or cold outside, or I can put on my shoes and walk out to see for myself. Sometimes, it is best to check on things, rather than to base opinions on preconceived notions—such as animals that hop are bunnies, or that the weather app really knows whether I will feel hot or cold outside.

I can use regular expressions, but dude…

When it comes to determining if a variable is null, empty, or something else, I often think about using regular expressions. In reality, I do not need to do that—especially not in this particular case. This is because System.String has a couple of static methods that are perfect for the occasion.

A static method is one that is always available, and it does not rely on a specific instance of the class before it comes into existence. For example, if I create a string, I have an instance of the String class. But if I don’t create a string, I can still access static string methods and properties directly from the class.

To see what static methods and properties are available, I can pipe [string] to Get-Member, for example:

PS C:\> [string] | Get-Member -Static

   TypeName: System.String

Name               MemberType Definition

—-                   ———- ———-

Compare            Method     static int Compare(string strA, string strB), stati…

CompareOrdinal     Method     static int CompareOrdinal(string strA, string strB)…

Concat             Method     static string Concat(System.Object arg0), static st…

Copy               Method     static string Copy(string str)

Equals             Method     static bool Equals(string a, string b), static bool…

Format             Method     static string Format(string format, System.Object a…

Intern             Method     static string Intern(string str)

IsInterned         Method     static string IsInterned(string str)

IsNullOrEmpty      Method     static bool IsNullOrEmpty(string value)

IsNullOrWhiteSpace Method     static bool IsNullOrWhiteSpace(string value)

Join               Method     static string Join(string separator, Params string[…

new                Method     string new(System.Char*, mscorlib, Version=4.0.0.0,…

ReferenceEquals    Method     static bool ReferenceEquals(System.Object objA, Sys…

Empty              Property   static string Empty {get;}

PS C:\>

One thing that is cool is that there is an IsNullOrEmpty static method. Here is an example of using that static property:

PS C:\> $a = "string a"

PS C:\> [string]::IsNullOrEmpty($a)

False

As I would expect, the string contained in the $a variable is not null, nor is it empty. If I use a variable that is not yet created, I obtain a different result:

PS C:\> [string]::IsNullOrEmpty($z)

True

What if I initialize the $b variable, but I do so with an empty string?

PS C:\> $b = " "

PS C:\> [string]::IsNullOrEmpty($b)

False

This time, my empty string comes back and says that it is not null, nor is it empty. It contains a blank space. What if I remove that space?

PS C:\> $b = ""

PS C:\> [string]::IsNullOrEmpty($b)

True

It seems that removing the space caused it to be null or empty.

Well, what about white space like the one I had earlier with the $b? As shown here, there is a method called IsNullOrWhiteSpace:

PS C:\> $c = " "

PS C:\> [string]::IsNullOrWhiteSpace($c)

True

These commands and their output are shown here:

Image of command output

JB, that is all there is to determining if a string is null or empty.  Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon