{"id":34,"date":"2019-12-16T14:00:44","date_gmt":"2019-12-16T14:00:44","guid":{"rendered":"https:\/\/www.practicalpowershell.com\/post\/null-empty-space-how-to-detect"},"modified":"2020-03-06T16:51:13","modified_gmt":"2020-03-06T16:51:13","slug":"null-empty-space-how-to-detect","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/","title":{"rendered":"Null.  Empty. Space? How to Detect?"},"content":{"rendered":"<p><strong>** Disclaimer **<\/strong> I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However, what follows is based on my understanding and personal experience.<\/p>\n<p><em>Let the blog begin. <\/em><\/p>\n<p>In scripts that I write, I sometimes need to detect an absence of value. By absence I mean either an empty variable, a variable with a space in it or some other indication that a value expected was not retrieved and thus we need to throw an error OR make a decision based on something missing. Why is this such a hard thing? Based on my experience, the absence is not always detectable with the $Null variable while looking for an empty variable in PowerShell. That being said, let&#8217;s review the many methods that can be used to find empty values.<\/p>\n<p><strong>Methods<\/strong><\/p>\n<p>Here are some sample ways to find a variable that is empty or has no content or value after trying to store something in it.<\/p>\n<p><strong>(1) $Null comparison<\/strong><\/p>\n<p>$Null &#8211; Using the $Null variable to compare against another variable:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Null -eq $MyVariable) { } Else { }<br \/>\n[\/sourcecode]<br \/>\nPlease note that the $Null variable is on the left side as per best practices. Also not that not every empty variable reads as Null. This using the other methods below may apply better to a particular scenario.<\/p>\n<p><strong>(2) Empty (No Spaces)<\/strong><\/p>\n<p>We can also check to see if the value is empty using quotes. Make sure not to put a space in-between the quotes for this check.<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($MyVariable -Eq &#039;&#039;) { } Else { }<br \/>\n[\/sourcecode]<br \/>\nNow alternatively, sometimes we get a space inserted into the value of the variable.<\/p>\n<p><strong>(3) Spaces Present and Not Null<\/strong><\/p>\n<p>We can check for it similarly to the above check:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($MyVariable -Eq &#039; &#039;) { } Else { }<br \/>\n[\/sourcecode]<br \/>\nThis check specifically looks for variables that were populated with a space instead of left as $Null or populated with usable data.<\/p>\n<p><strong>(4) .Net feature &#8211; IsNullOrWhiteSpace<\/strong><\/p>\n<p>Now lastly, we can combine a couple of checks like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf\u00a0(![string]::IsNullOrWhiteSpace($MyVariable))\u00a0{ } Else { }<br \/>\n[\/sourcecode]<br \/>\nThe above test is a hybrid of two of the previous checks and IMHO works better than some other checks that I have used. That being said, there is some sort of performance hit from using this. Just be aware that this is not the fastest, but it is the most flexible in terms of what it can matched. \u2026<\/p>\n<p>and that&#8217;s it for this article. Hopefully you find this tip to be useful for when you are trying to discover if a value is truly empty, a whitespace or something more valuable.<\/p>\n<p><strong>Further Reading<\/strong><\/p>\n<p>* <a href=\"https:\/\/powershellexplained.com\/2018-12-23-Powershell-null-everything-you-wanted-to-know\/\">$Null<\/a><br \/>\n* <a href=\"https:\/\/weblogs.asp.net\/gunnarpeipman\/net-framework-4-0-string-isnullorwhitespace-method\">IsNullOrWhiteSpace<\/a><br \/>\n* <a href=\"https:\/\/referencesource.microsoft.com\/#mscorlib\/system\/string.cs,23a8597f842071f4\">Code Reference<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>** Disclaimer ** I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However, what follows is based on my understanding and personal experience. Let the blog begin. In scripts that I write, I sometimes need to detect an absence of value. By absence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Null. Empty. Space? How to Detect? - Practical PowerShell<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Null. Empty. Space? How to Detect? - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"** Disclaimer ** I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However, what follows is based on my understanding and personal experience. Let the blog begin. In scripts that I write, I sometimes need to detect an absence of value. By absence [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-16T14:00:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-06T16:51:13+00:00\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Null. Empty. Space? How to Detect?\",\"datePublished\":\"2019-12-16T14:00:44+00:00\",\"dateModified\":\"2020-03-06T16:51:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/\"},\"wordCount\":450,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/\",\"name\":\"Null. Empty. Space? How to Detect? - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"datePublished\":\"2019-12-16T14:00:44+00:00\",\"dateModified\":\"2020-03-06T16:51:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/null-empty-space-how-to-detect\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Null. Empty. Space? How to Detect?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Null. Empty. Space? How to Detect? - Practical PowerShell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/","og_locale":"en_US","og_type":"article","og_title":"Null. Empty. Space? How to Detect? - Practical PowerShell","og_description":"** Disclaimer ** I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However, what follows is based on my understanding and personal experience. Let the blog begin. In scripts that I write, I sometimes need to detect an absence of value. By absence [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/","og_site_name":"Practical PowerShell","article_published_time":"2019-12-16T14:00:44+00:00","article_modified_time":"2020-03-06T16:51:13+00:00","author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Null. Empty. Space? How to Detect?","datePublished":"2019-12-16T14:00:44+00:00","dateModified":"2020-03-06T16:51:13+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/"},"wordCount":450,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/","url":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/","name":"Null. Empty. Space? How to Detect? - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"datePublished":"2019-12-16T14:00:44+00:00","dateModified":"2020-03-06T16:51:13+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/null-empty-space-how-to-detect\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Null. Empty. Space? How to Detect?"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":3,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/34\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}