{"id":3847,"date":"2013-04-11T00:01:00","date_gmt":"2013-04-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/11\/named-arguments-for-powershell-functions-best-practices\/"},"modified":"2013-04-11T00:01:00","modified_gmt":"2013-04-11T00:01:00","slug":"named-arguments-for-powershell-functions-best-practices","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/named-arguments-for-powershell-functions-best-practices\/","title":{"rendered":"Named Arguments for PowerShell Functions: Best Practices"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using named arguments in Windows PowerShell functions.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. If I go to the trouble of writing a Windows PowerShell script, I generally do not use unnamed arguments (such as <strong>$args<\/strong> as I illustrated yesterday in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/04\/10\/accepting-arguments-for-powershell-functions-best-practices.aspx\" target=\"_blank\">Accepting Arguments for PowerShell Functions: Best Practices<\/a>). Instead I create named arguments for my functions. It is just so much more powerful, and so much more flexible. Besides, I can still pass values appositionally in an unnamed fashion if I wish to do so.<\/p>\n<h2>Create a named argument in five easy steps<\/h2>\n<p>In yesterday&#8217;s blog,&nbsp;I said that there are only three requirements to create a function in Windows PowerShell:<\/p>\n<ol>\n<li>The <strong>Function<\/strong> keyword<\/li>\n<li>The name of the function<\/li>\n<li>A script block<\/li>\n<\/ol>\n<p>To create a named argument in a Windows PowerShell function, I need only two &nbsp;additional things:<\/p>\n<ol>\n<li>The <strong>Param<\/strong> keyword<\/li>\n<li>A variable to hold the argument inside a pair of parentheses<\/li>\n<\/ol>\n<p>The following script illustrates this technique:<\/p>\n<p style=\"padding-left: 30px\">Function myfunction<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Param($myargument)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;This value of `$myargument is $myargument&#8221;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p><span style=\"font-size: small\">To use <strong style=\"font-size: 12px\">MyFunction<\/strong>, I first have to run the script. This loads the function into memory and makes it available via the function PSDrive. Because I have not saved the script containing the function, when I run the script, it appears in the Console pane below the Script pane. When the script runs, the Windows PowerShell prompt returns, and I can call the function by typing the name of the function. I then supply a value for the argument by typing it. This is shown in the image that follows.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4617.hsg-4-11-13-01.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4617.hsg-4-11-13-01.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>Keep in mind that tab expansion works here. So I do not have to type the entire name of <strong>MyFunction<\/strong>, nor do I need to type the complete name of <strong>MyArgument<\/strong><em>. <\/em>In fact, I only had to type <strong>my<\/strong> and press the Tab key to get the <strong>MyFunction<\/strong><em> <\/em>command onto the command line. When I type the hyphen (<strong>&ndash;<\/strong>) for the named argument (parameter) a pop-up list appears, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6153.hsg-4-11-13-02.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6153.hsg-4-11-13-02.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>The advantage of using named arguments (parameters) is that I do not need to name the parameter if I do not want to. I can use it as a positional parameter. In this manner, it behaves like an unnamed argument (<strong>$args<\/strong>). This is shown here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\">PS C:\\&gt; myfunction &#8220;this is a&nbsp; string&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\">This value of $myargument is this is a&nbsp; string<\/span><\/p>\n<p><span style=\"font-size: small\">Because creating named parameters in Windows PowerShell is so easy, and because using the <strong style=\"font-size: 12px\">Param<\/strong><em style=\"font-size: 12px\"> <\/em>keyword is the entry into the world of advanced functions, I never use <strong style=\"font-size: 12px\">$args<\/strong> in a Windows PowerShell script. Because it is an automatic variable that becomes available in certain circumstances, using <strong style=\"font-size: 12px\">$args<\/strong> is more difficult to understand because nothing has been created in the script. It is just there.<\/span><\/p>\n<p><span style=\"font-size: small\">On the other hand, because the <strong>Param<\/strong><em> <\/em>block is declared and available for inspection, it makes sense, and is easier to understand. If I begin with a script that uses <strong>$args<\/strong> and I later decide that I need to add functionality, I will have to add a <strong>Param<\/strong><em> <\/em>block to get access to advanced features.<\/span><\/p>\n<p>Join me tomorrow when I will welcome guest bloggers Yuri Diogenes and Tom Shinder back with the second installment in their security series. If you want to refresh your memory, check out their first installment in the series:<\/p>\n<p style=\"padding-left: 30px\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/29\/security-series-using-powershell-to-protect-your-private-cloud-infrastructure.aspx\" target=\"_blank\">Security Series: Using PowerShell to Protect Your Private Cloud Infrastructure<\/a><\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using named arguments in Windows PowerShell functions. Microsoft Scripting Guy, Ed Wilson, is here. If I go to the trouble of writing a Windows PowerShell script, I generally do not use unnamed arguments (such as $args as I illustrated yesterday in Accepting Arguments for PowerShell Functions: Best [&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":[331,69,3,4,45],"class_list":["post-3847","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-best-practices","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using named arguments in Windows PowerShell functions. Microsoft Scripting Guy, Ed Wilson, is here. If I go to the trouble of writing a Windows PowerShell script, I generally do not use unnamed arguments (such as $args as I illustrated yesterday in Accepting Arguments for PowerShell Functions: Best [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3847","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=3847"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3847\/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=3847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}