{"id":48,"date":"2019-07-01T16:19:45","date_gmt":"2019-07-01T16:19:45","guid":{"rendered":"https:\/\/www.practicalpowershell.com\/post\/conditional-variables"},"modified":"2020-03-09T18:17:10","modified_gmt":"2020-03-10T00:17:10","slug":"conditional-variables","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/conditional-variables\/","title":{"rendered":"Conditional Variables"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?resize=150%2C150&#038;ssl=1\" alt=\"\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-329\" srcset=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?resize=100%2C100&amp;ssl=1 100w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?zoom=2&amp;resize=150%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?zoom=3&amp;resize=150%2C150&amp;ssl=1 450w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>Making decisions in PowerShell.  In this weeks PowerShell tip of the week we will review ways we can use variables to help drive scripts conditionally.  This means that when a variable it set to a certain value then a subsection code its either run or skipped.  Thus we can use variables conditionally to make this happen.<BR><br \/>\n<strong>Example 1<\/strong><BR><br \/>\nFor this sample code, a PowerShell query is made using CIMInstance and it fails, a good fallback is Get-WMIObject. We can use a combo of Try and Catch and a variable to store a result. The variable will be used to either trigger a WMI query (if the CIM query fails) or to ignore the WMI query if the CIM query worked as expected.<BR><br \/>\nFirst section of code attempts to query a server via a CIM Instance. If the query failed (Inside the Try section) the code section in the Catch section will be run, and in this case it will set a $WMIQuery variable to $True:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nTry {<br \/>\n    $MaximumSize = (Get-CimInstance -ComputerName $Server -Query &quot;Select * from win32_PageFileSetting&quot; -ErrorAction STOP| select-object MaximumSize).MaximumSize<br \/>\n} Catch {<br \/>\n    $WMIQuery = $True<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nRight after that the CIM query section, we can check the $WMIQuery variable to see it is true. If the variable is true (which it would be if the CIM query fails), then this code is executed:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($WMIQuery) { <\/p>\n<p>    Try {<br \/>\n        $MaximumSize = (Get-WMIObject -ComputerName $Server -Class win32_PageFileSetting -ErrorAction STOP | select-object MaximumSize).MaximumSize<br \/>\n    } Catch {<br \/>\n        $MaximumSize = &#039;Managed&#039;<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nConditional variables can be used to create a determined flow in a script as well as be a part of error handling (Try {} Catch{}).<BR><br \/>\n<strong>Example 2<\/strong><BR><br \/>\nIn this next sample code block we will first set a variable ($Subscriptions) to $False. Then we will run a section of code to check to see if there are Edge Subscriptions (Edge Servers present in Exchange) and if the query fails, register this as no Edge subscriptions.<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$Subscriptions = $False Try { $EdgeSubscriptions = Get-EdgeSubscription -ErrorAction Stop | Select Name,Site,Domain,IsValid $Subscriptions = $True } Catch { $Line = &#039;No Edge Transport Servers or Subscriptions found.&#039; | Out-File $Destination -Append }<br \/>\n[\/sourcecode]<br \/>\nIf the Get-EdgeSubscription succeeds and the $Subscriptions variable is changed from $False to $True. Once that variable set, we move on to another code section which will ONLY execute if the $Subscriptions variable is $True:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Subscriptions) { (Code to run) }<br \/>\n[\/sourcecode]<br \/>\nThis last section of code could be written a couple of ways:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Subscriptions) { }<br \/>\n[\/sourcecode]<br \/>\nOr<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Subscriptions -eq $True) {}<br \/>\n[\/sourcecode]<br \/>\nEither way, if the $Subscriptions variable is $True, the code section in the brackets ( { } ) will execute. Now, we can also execute code if a variable false:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf (!(Subscriptions) { }<br \/>\n[\/sourcecode]<br \/>\nOr<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Subscriptions -eq $False) {}<br \/>\n[\/sourcecode]<br \/>\n<strong>** NOTE ** <\/strong>  One thing to be careful of is that some cmdlets may not fail and only return a Null result, which would defeat the purpose of the Try{} Catch{} error handling.  In cases like that, we may want to have an IF () check to see if a variable is Null instead.  Still conditional because we are looking for a value to determine a course of action.<BR><br \/>\n<strong>Example 3 <\/strong><BR><br \/>\nIn this last example, we can execute a code block based on what version of Exchange (or Windows) is running on a server. The first code section will determine the value for the $ExVersion variable:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$ExVersion = $Null<br \/>\nIf ($Version -like &#039;*15.2*&#039;) {$ExVersion = &#039;2019&#039;}<br \/>\nIf ($Version -like &#039;*15.1*&#039;) {$ExVersion = &#039;2016&#039;}<br \/>\nIf ($Version -like &#039;*15.0*&#039;) {$ExVersion = &#039;2013&#039;}<br \/>\nIf ($Version -like &#039;*14.*&#039;) {$ExVersion = &#039;2010&#039;}<br \/>\nIf ($Version -like &#039;*8.*&#039;) {$ExVersion = &#039;2007&#039;}<br \/>\n[\/sourcecode]<br \/>\nOnce this variable is populated, we can then run a subset of code based on the value of  the $ExVersion variable:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($ExVersion -eq 2019) { }<br \/>\nIf ($ExVersion -eq 2016) { }<br \/>\nIf ($ExVersion -eq 2013) { }<br \/>\n[\/sourcecode]<br \/>\nAs we can see by the above examples, using a variables value to determine  the direction of the script and what code to execute can be useful in some scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making decisions in PowerShell. In this weeks PowerShell tip of the week we will review ways we can use variables to help drive scripts conditionally. This means that when a variable it set to a certain value then a subsection code its either run or skipped. Thus we can use variables conditionally to make this [&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-48","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>Conditional Variables - 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\/conditional-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conditional Variables - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"Making decisions in PowerShell. In this weeks PowerShell tip of the week we will review ways we can use variables to help drive scripts conditionally. This means that when a variable it set to a certain value then a subsection code its either run or skipped. Thus we can use variables conditionally to make this [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/conditional-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-01T16:19:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-10T00:17:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional-150x150.jpg\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Conditional Variables\",\"datePublished\":\"2019-07-01T16:19:45+00:00\",\"dateModified\":\"2020-03-10T00:17:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/\"},\"wordCount\":656,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Conditional-150x150.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/\",\"name\":\"Conditional Variables - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Conditional-150x150.jpg\",\"datePublished\":\"2019-07-01T16:19:45+00:00\",\"dateModified\":\"2020-03-10T00:17:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Conditional.jpg?fit=623%2C398&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Conditional.jpg?fit=623%2C398&ssl=1\",\"width\":623,\"height\":398},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/conditional-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Conditional Variables\"}]},{\"@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":"Conditional Variables - 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\/conditional-variables\/","og_locale":"en_US","og_type":"article","og_title":"Conditional Variables - Practical PowerShell","og_description":"Making decisions in PowerShell. In this weeks PowerShell tip of the week we will review ways we can use variables to help drive scripts conditionally. This means that when a variable it set to a certain value then a subsection code its either run or skipped. Thus we can use variables conditionally to make this [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/conditional-variables\/","og_site_name":"Practical PowerShell","article_published_time":"2019-07-01T16:19:45+00:00","article_modified_time":"2020-03-10T00:17:10+00:00","og_image":[{"url":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional-150x150.jpg","type":"","width":"","height":""}],"author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Conditional Variables","datePublished":"2019-07-01T16:19:45+00:00","dateModified":"2020-03-10T00:17:10+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/"},"wordCount":656,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"image":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional-150x150.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/conditional-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/conditional-variables\/","url":"https:\/\/practicalpowershell.com\/conditional-variables\/","name":"Conditional Variables - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#primaryimage"},"image":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional-150x150.jpg","datePublished":"2019-07-01T16:19:45+00:00","dateModified":"2020-03-10T00:17:10+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/conditional-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#primaryimage","url":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?fit=623%2C398&ssl=1","contentUrl":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/07\/Conditional.jpg?fit=623%2C398&ssl=1","width":623,"height":398},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/conditional-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Conditional Variables"}]},{"@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\/48","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=48"}],"version-history":[{"count":3,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/48\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}