{"id":76951,"date":"2016-02-17T11:59:38","date_gmt":"2016-02-17T11:59:38","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=76951"},"modified":"2019-02-18T09:19:50","modified_gmt":"2019-02-18T16:19:50","slug":"powertip-prompt-user-for-value-but-provide-default","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-prompt-user-for-value-but-provide-default\/","title":{"rendered":"PowerTip: Prompt user for value but provide default"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud &amp; Datacenter Management MVP, Thomas Rayner, shows how to set a variable to a default value in PowerShell.<\/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\" \/>\u00a0How can I use Windows PowerShell to prompt a user for a value for a variable, but provide a default value if the user doesn\u2019t enter anything?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0First prompt the user for the value to the variable and then detect if the response was empty. If the response is empty, set the value of the variable. You can validate parameters for a function more conveniently than this. This code is for inline variables in an interactive script:<\/p>\n<p style=\"padding-left: 90px\">$Interesting = Read-Host -Prompt &#8216;Give me a value [some-value]&#8217;<\/p>\n<p style=\"padding-left: 90px\">if ([string]::IsNullOrWhiteSpace($Interesting))<\/p>\n<p style=\"padding-left: 90px\">{<\/p>\n<p style=\"padding-left: 90px\">$Interesting = &#8216;some-value&#8217;<\/p>\n<p style=\"padding-left: 90px\">}<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\" \" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Cloud &amp; Datacenter Management MVP, Thomas Rayner, shows how to set a variable to a default value in PowerShell. \u00a0How can I use Windows PowerShell to prompt a user for a value for a variable, but provide a default value if the user doesn\u2019t enter anything? \u00a0First prompt the user for the value 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":[639],"tags":[56,356,3,652,45],"class_list":["post-76951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powertip","tag-guest-blogger","tag-powertip","tag-scripting-guy","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Cloud &amp; Datacenter Management MVP, Thomas Rayner, shows how to set a variable to a default value in PowerShell. \u00a0How can I use Windows PowerShell to prompt a user for a value for a variable, but provide a default value if the user doesn\u2019t enter anything? \u00a0First prompt the user for the value to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76951","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=76951"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76951\/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=76951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=76951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=76951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}