{"id":72901,"date":"2015-10-07T00:01:00","date_gmt":"2015-10-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/10\/07\/dude-a-string-is-a-string-in-powershell\/"},"modified":"2019-02-18T09:34:59","modified_gmt":"2019-02-18T16:34:59","slug":"dude-a-string-is-a-string-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dude-a-string-is-a-string-in-powershell\/","title":{"rendered":"Dude, a String Is a String in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about using string methods to determine null or empty in Windows PowerShell.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;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 <b>$null<\/b>, but this does not work very well. I need a different way to do this. Can you help?<\/p>\n<p>&mdash;JB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JB,<\/p>\n<p>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.<\/p>\n<p>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&mdash;such as animals that hop are bunnies, or that the weather app really knows whether I will feel hot or cold outside.<\/p>\n<h2>I can use regular expressions, but dude&hellip;<\/h2>\n<p>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&mdash;especially not in this particular case. This is because <b>System.String<\/b> has a couple of static methods that are perfect for the occasion.<\/p>\n<p>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 <b>String<\/b> class. But if I don&rsquo;t create a string, I can still access static string methods and properties directly from the class.<\/p>\n<p>To see what static methods and properties are available, I can pipe <b>[string]<\/b> to <b>Get-Member<\/b>, for example:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string] | Get-Member -Static<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: System.String<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Compare&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static int Compare(string strA, string strB), stati&#8230;<\/p>\n<p style=\"margin-left:30px\">CompareOrdinal&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static int CompareOrdinal(string strA, string strB)&#8230;<\/p>\n<p style=\"margin-left:30px\">Concat&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static string Concat(System.Object arg0), static st&#8230;<\/p>\n<p style=\"margin-left:30px\">Copy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static string Copy(string str)<\/p>\n<p style=\"margin-left:30px\">Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static bool Equals(string a, string b), static bool&#8230;<\/p>\n<p style=\"margin-left:30px\">Format&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static string Format(string format, System.Object a&#8230;<\/p>\n<p style=\"margin-left:30px\">Intern&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static string Intern(string str)<\/p>\n<p style=\"margin-left:30px\">IsInterned&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;&nbsp;&nbsp;&nbsp; static string IsInterned(string str)<\/p>\n<p style=\"margin-left:30px\">IsNullOrEmpty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static bool IsNullOrEmpty(string value)<\/p>\n<p style=\"margin-left:30px\">IsNullOrWhiteSpace Method&nbsp;&nbsp;&nbsp;&nbsp; static bool IsNullOrWhiteSpace(string value)<\/p>\n<p style=\"margin-left:30px\">Join&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static string Join(string separator, Params string[&#8230;<\/p>\n<p style=\"margin-left:30px\">new&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; string new(System.Char*, mscorlib, Version=4.0.0.0,&#8230;<\/p>\n<p style=\"margin-left:30px\">ReferenceEquals&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static bool ReferenceEquals(System.Object objA, Sys&#8230;<\/p>\n<p style=\"margin-left:30px\">Empty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; static string Empty {get;}<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>One thing that is cool is that there is an <b>IsNullOrEmpty<\/b> static method. Here is an example of using that static property:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;string a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::IsNullOrEmpty($a)<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p>As I would expect, the string contained in the <b>$a<\/b> variable is not null, nor is it empty. If I use a variable that is not yet created, I obtain a different result:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::IsNullOrEmpty($z)<\/p>\n<p style=\"margin-left:30px\">True<\/p>\n<p>What if I initialize the <b>$b<\/b> variable, but I do so with an empty string?<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b = &quot; &quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::IsNullOrEmpty($b)<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p>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?<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b = &quot;&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::IsNullOrEmpty($b)<\/p>\n<p style=\"margin-left:30px\">True<\/p>\n<p>It seems that removing the space caused it to be null or empty.<\/p>\n<p>Well, what about white space like the one I had earlier with the <b>$b<\/b>? As shown here, there is a method called <b>IsNullOrWhiteSpace<\/b>:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $c = &quot; &quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::IsNullOrWhiteSpace($c)<\/p>\n<p style=\"margin-left:30px\">True<\/p>\n<p>These commands and their output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2161.hsg-10-7-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2161.hsg-10-7-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>JB, that is all there is to determining if a string is null or empty.&nbsp; Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using string methods to determine null or empty in Windows PowerShell. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,336,45],"class_list":["post-72901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using string methods to determine null or empty in Windows PowerShell. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=72901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=72901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}