{"id":27,"date":"2010-12-21T23:28:26","date_gmt":"2010-12-22T06:28:26","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=27"},"modified":"2010-12-21T23:28:26","modified_gmt":"2010-12-22T06:28:26","slug":"quick-tip-script-parameters-in-powershell","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=27","title":{"rendered":"Quick Tip: Script Parameters in PowerShell"},"content":{"rendered":"<p>If you are writing a script that you want to (might) use again it can be really useful to use script parameters. PowerShell does some really cool things with parameters that many folks don&#8217;t know or realize you can do. This post covers some of those items.<\/p>\n<p>To start us off let&#8217;s look at how to use a script parameter.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"co1\">### Test-Param.ps1 ###<\/span><br \/>\n<span class=\"kw3\">Param<\/span><span class=\"br0\">&#40;<\/span><span class=\"re0\">$Variable1<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Hello&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"re0\">$Variable2<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;World&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"st0\">&quot;$Variable1 $Variable2&quot;<\/span><\/div><\/div>\n<p>If we save this script and run it we will get an output like this:<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"sy0\">&gt;&gt;<\/span>.<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span>Hello World<\/div><\/div>\n<p>This simple case shows that our variables are being assigned to the default values.  We can change these values by passing them to the script via the command line.  These values are passed either in the order that they are sent, or statically assigned by using the variables name.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"sy0\">&gt;&gt;<\/span>.<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 <span class=\"st0\">&quot;Goodbye&quot;<\/span><br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Goodbye World<br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> .<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 <span class=\"sy0\">-<\/span>Variable2 <span class=\"st0\">&quot;Universe&quot;<\/span> <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Hello Universe <br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> .<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 <span class=\"st0\">&quot;Universe&quot;<\/span> <span class=\"sy0\">-<\/span>Variable1 <span class=\"st0\">&quot;Goodbye&quot;<\/span><br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Goodbye Universe<\/div><\/div>\n<p>As you can see, these are really pretty flexible. Another really cool part of params that you may have noticed by now if you are following along is that these variable names will use tab complete. So if you type &#8220;.\/test-param -&#8221; and then start hitting tab, you will cycle through all the names in the param block. If yo haven&#8217;t used a script in a long time, then you can quickly see what variables you might want to pass.<\/p>\n<p>Now, sometimes you want to have the param feature, but you need to require the value. You can place some logic after the param block, but I like to just put code straight in there.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"co1\">### Test-Param.ps1 ###<\/span><br \/>\n<span class=\"kw3\">Param<\/span><span class=\"br0\">&#40;<\/span><span class=\"re0\">$Variable1<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Read-Host<\/span> <span class=\"st0\">&quot;Please input a value for Variable1&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"re0\">$Variable2<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Content<\/span> <span class=\"re0\">$Variable1<\/span><br \/>\n<span class=\"st0\">&quot;$Variable1&quot;<\/span><br \/>\n<span class=\"st0\">&quot;$Variable2&quot;<\/span> <br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> .<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Please input a value <span class=\"kw3\">for<\/span> Variable1: c:ScriptTest.txt<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> c:ScriptTest.txt <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Hello<span class=\"sy0\">,<\/span> World<span class=\"sy0\">!<\/span> <br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> .<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 c:ScriptTest.txt <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> c:ScriptTest.txt <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Hello<span class=\"sy0\">,<\/span> World<span class=\"sy0\">!<\/span> <br \/>\n<br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> .<span class=\"sy0\">\/<\/span>Test<span class=\"sy0\">-<\/span><span class=\"kw3\">Param<\/span>.ps1 c:ScriptTest.txt <span class=\"st0\">&quot;Ignore my file&quot;<\/span> <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> c:ScriptTest.txt <br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Ignore my file<\/div><\/div>\n<p>I think that just about covers it for this Quick Tip. Get started converting all your scripts to use a parameter block!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trying to figure out how to pass values to a script?  Parameters!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/27"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}