{"id":3852,"date":"2013-04-10T00:01:00","date_gmt":"2013-04-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/10\/accepting-arguments-for-powershell-functions-best-practices\/"},"modified":"2013-04-10T00:01:00","modified_gmt":"2013-04-10T00:01:00","slug":"accepting-arguments-for-powershell-functions-best-practices","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/accepting-arguments-for-powershell-functions-best-practices\/","title":{"rendered":"Accepting 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 the best practices surrounding accepting input for a Windows PowerShell function.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. April in the Carolina&rsquo;s is a special time. In fact, it is my favorite time of the year here. This is because the weather is invariably mild. This week, it has been sunny, moderate temperature, mild humidity, and clear skies. The Scripting Neighbors tell me it is perfect golf weather. It is also perfect &ldquo;sit on the lanai and write Windows PowerShell scripts&rdquo; weather. Although I have never had much luck with <a href=\"http:\/\/quoteinvestigator.com\/2011\/08\/01\/golf-small-hole\/\" target=\"_blank\">putting a small ball into an even smaller hole with equipment not designed for that purpose<\/a>, I can compute the trajectory, and force necessary to accomplish the task with a one-line Windows PowerShell command.<\/p>\n<h2>Passing a value to a Windows PowerShell function<\/h2>\n<p>If I have a function that I need to pass a value to, I can use the automatic variable <strong>$args<\/strong>. This makes the function easy to write and easy to use. The following function uses three steps to create the function. It calls the function keyword, provides a name, and creates a script block that contains code.<\/p>\n<p style=\"padding-left: 30px\">function myfunction<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;the computer name is $args&#8221;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>In the Windows PowerShell ISE, I run the script (I do not have to save the code into a .ps1 file), and the function loads into memory. I can then call the function directly in the execution pane (the dark blue box that follows) and pass a value to the function when I call it. The command line is shown here:<\/p>\n<p style=\"padding-left: 30px\">myfunction $env:COMPUTERNAME<\/p>\n<p>The image that follows illustrates creating the function, using <strong>$args<\/strong> in the script block, and calling the function from the execution pane.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0880.hsg-4-10-13-01.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of command\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0880.hsg-4-10-13-01.png\" alt=\"Image of command\" \/><\/a><\/p>\n<p>Anything I add following the name of the function populates the <strong>$args<\/strong> variable. In the command that follows, I pass the value <strong>mred<\/strong><em> <\/em>to the function. Interestingly, I do not have to supply quotation marks when passing the value.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; myfunction mred<\/p>\n<p style=\"padding-left: 30px\">the computer name is mred<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; myfunction &#8220;mred&#8221;<\/p>\n<p style=\"padding-left: 30px\">the computer name is mred<\/p>\n<p>I can also use the output from the <strong>Get-WmiObject<\/strong> cmdlet for input. Therefore, the following code uses WMI to return the computer name and to pass it to the <strong>MyFunction<\/strong><em> <\/em>function.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; myfunction (gwmi win32_computersystem).name<\/p>\n<p style=\"padding-left: 30px\">the computer name is EDLT<\/p>\n<p>One thing to keep in mind, is that when I use the <strong>$args<\/strong> automatic variable as illustrated in the <strong>MyFunction<\/strong><em> <\/em>function, I cannot pipe input to the function. This can actually be a bit of a problem, because it could be really hard to troubleshoot due to the fact that no error arises. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $env:COMPUTERNAME | myfunction<\/p>\n<p style=\"padding-left: 30px\">the computer name is&nbsp;<\/p>\n<p>If I want to pipel input to my function, I use the <strong>$input<\/strong> automatic variable. The only change that is required to my function is to change <strong>$args<\/strong> to <strong>$input<\/strong>, as shown here.<\/p>\n<p style=\"padding-left: 30px\">function afunction<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&#8220;the computer name is $input&#8221;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>I then pipe the input to the function by using the command shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $env:COMPUTERNAME | afunction<\/p>\n<p>If I attempt to provide positional input to the function instead of piping the input, no error arises, but no value passes either.<\/p>\n<p>The command and associated output are shown in the image here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8880.hsg-4-10-13-02.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8880.hsg-4-10-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Best Practices Week will continue tomorrow when I will talk some more about Windows PowerShell functions.<\/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><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about the best practices surrounding accepting input for a Windows PowerShell function. Microsoft Scripting Guy, Ed Wilson, is here. April in the Carolina&rsquo;s is a special time. In fact, it is my favorite time of the year here. This is because the weather is invariably mild. This week, [&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-3852","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 the best practices surrounding accepting input for a Windows PowerShell function. Microsoft Scripting Guy, Ed Wilson, is here. April in the Carolina&rsquo;s is a special time. In fact, it is my favorite time of the year here. This is because the weather is invariably mild. This week, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3852","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=3852"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3852\/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=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}