{"id":76881,"date":"2016-02-16T11:59:24","date_gmt":"2016-02-16T11:59:24","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=76881"},"modified":"2019-02-18T09:19:51","modified_gmt":"2019-02-18T16:19:51","slug":"powertip-use-positional-parameters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-use-positional-parameters\/","title":{"rendered":"PowerTip: Use positional parameters"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud &amp; Datacenter Management MVP, Thomas Rayner, shows how to use positional parameters in Windows 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\" \/>\u00a0I always see people pass variables or data to a function without specifying the parameter name that the data is for, for example:<\/p>\n<p style=\"padding-left: 30px\">\u201c<strong>copy-item $source $destination<\/strong>\u201d acts like \u201c<strong>copy-item \u2013path $source \u2013destination $destination<\/strong>\u201d<\/p>\n<p>How do I do that in my functions, and how does the function know which variable is the path and destination?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" class=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0In functions, you declare parameters before doing much else. When declaring your parameter, indicate the <strong>Position <\/strong>starting with 0, for example:<\/p>\n<p style=\"padding-left: 120px\">Function Invoke-Test<\/p>\n<p style=\"padding-left: 120px\">{<\/p>\n<p style=\"padding-left: 120px\">[CmdletBinding()]<\/p>\n<p style=\"padding-left: 120px\">Param(<\/p>\n<p style=\"padding-left: 120px\">[Parameter(Position = 0)]<\/p>\n<p style=\"padding-left: 120px\">[string]$First,<\/p>\n<p style=\"padding-left: 120px\">[Parameter(Position = 1)]<\/p>\n<p style=\"padding-left: 120px\">[string]$Second<\/p>\n<p style=\"padding-left: 120px\">)<\/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 use positional parameters in Windows PowerShell. \u00a0I always see people pass variables or data to a function without specifying the parameter name that the data is for, for example: \u201ccopy-item $source $destination\u201d acts like \u201ccopy-item \u2013path $source \u2013destination $destination\u201d How do I do that [&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-76881","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 use positional parameters in Windows PowerShell. \u00a0I always see people pass variables or data to a function without specifying the parameter name that the data is for, for example: \u201ccopy-item $source $destination\u201d acts like \u201ccopy-item \u2013path $source \u2013destination $destination\u201d How do I do that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76881","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=76881"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76881\/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=76881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=76881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=76881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}