{"id":4911,"date":"2009-01-02T16:03:37","date_gmt":"2009-01-02T16:03:37","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/02\/how-and-why-to-use-splatting-passing-switch-parameters\/"},"modified":"2019-02-18T13:12:59","modified_gmt":"2019-02-18T20:12:59","slug":"how-and-why-to-use-splatting-passing-switch-parameters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/how-and-why-to-use-splatting-passing-switch-parameters\/","title":{"rendered":"How and Why to Use Splatting (passing [switch] parameters)"},"content":{"rendered":"<p>On our internal discussion list, someone just asked about how to pass switch parameters from one function to another.&#160; This person asked this as &quot;Why can&#8217;t [switch] parameters be passed as parameters?&quot;.<\/p>\n<p>My short answer was simple: &quot;They Can Be&quot;<\/p>\n<p>Answering this question is a great way to illustrate the value of a PowerShell V2 feature called Splatting.&#160; Splatting is two things, it&#8217;s a way to get all of the parameters passed to a function automatically (they&#8217;re in a variable, $psBoundParameters), and it&#8217;s a way to take a list or dictionary of arguments and pass them on to the next function below.<\/p>\n<p>The mistake of trying to pass along a switch parameter is a pretty easy one to make.&#160; [switch] will let you specify the parameter by just passing &#8211;<em>ParameterName<\/em> instead of &#8211;<em>ParameterName <\/em>True.<\/p>\n<p>When you try to pass on a value to another underlying function, &#8211;<em>SwitchParameterInOtherFunction <\/em>$switchParameterInThis function will set the switch to true in the underlying function, and then pass a positional parameter with the value of the switch parameter.<\/p>\n<p>The V1 fix for this is to put a : between the parameter name and its value.&#160; The V2 fix for this is to simply use splatting.&#160;&#160; The important line below is in bold and italics.<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"755\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"380\">V2:<\/td>\n<td valign=\"top\" width=\"373\">V1:<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"380\">\n<p>function test2($x, $y, [switch]$passThru) {           <br \/>&#160;&#160;&#160; &quot;In Test 2&quot;            <br \/>&#160;&#160;&#160; &quot;x:&quot;            <br \/>&#160;&#160;&#160; $x            <br \/>&#160;&#160;&#160; &quot;y:&quot;            <br \/>&#160;&#160;&#160; $y            <br \/>&#160;&#160;&#160; &quot;passThru:&quot;            <br \/>&#160;&#160;&#160; $passThru            <br \/>} <\/p>\n<p>function test1($x, $y, [switch]$passThru) {           <br \/>&#160;&#160;&#160; &quot;In Test 1&quot;            <br \/>&#160;&#160;&#160; <strong><em>Test2 @psBoundParameters<\/em><\/strong>            <br \/>} <\/p>\n<p>test1 &quot;a&quot; &quot;b&quot; -passThru<\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p>function test2($x, $y, [switch]$passThru) {           <br \/>&#160;&#160;&#160; &quot;In Test 2&quot;            <br \/>&#160;&#160;&#160; &quot;x:&quot;            <br \/>&#160;&#160;&#160; $x            <br \/>&#160;&#160;&#160; &quot;y:&quot;            <br \/>&#160;&#160;&#160; $y            <br \/>&#160;&#160;&#160; &quot;passThru:&quot;            <br \/>&#160;&#160;&#160; $passThru            <br \/>} <\/p>\n<p>function test1($x, $y, [switch]$passThru) {           <br \/>&#160;&#160;&#160; &quot;In Test 1&quot;            <br \/>&#160;&#160;&#160; <em><strong>Test2 -x:$x -y:$y -passThru:$passThru<\/strong><\/em>            <br \/>}<\/p>\n<\/p>\n<p>test1 &quot;a&quot; &quot;b&quot; -passThru<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So why use splatting?&#160; Well, here was my answer to the question on our PowerShell Discussions alias:<\/p>\n<p><em>Notice that with @psBoundParameters, I don&#8217;t have to specify the parameters on by one.&#160;&#160; <u>This is both more convenient (less typing) and less error prone (less chance of forgetting a parameter or adding a typo).<\/u><\/em><\/p>\n<p>To see more powerful examples of splatting, you can check out <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2008\/12\/25\/get-commandplugin.aspx\">Get-CommandPlugin<\/a> and the <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2008\/12\/25\/merry-christmas-from-powershell-the-codedownloader-module.aspx\">CodeDownloader<\/a> module.<\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On our internal discussion list, someone just asked about how to pass switch parameters from one function to another.&#160; This person asked this as &quot;Why can&#8217;t [switch] parameters be passed as parameters?&quot;. My short answer was simple: &quot;They Can Be&quot; Answering this question is a great way to illustrate the value of a PowerShell V2 [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[85,317],"class_list":["post-4911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-switch","tag-splatting"],"acf":[],"blog_post_summary":"<p>On our internal discussion list, someone just asked about how to pass switch parameters from one function to another.&#160; This person asked this as &quot;Why can&#8217;t [switch] parameters be passed as parameters?&quot;. My short answer was simple: &quot;They Can Be&quot; Answering this question is a great way to illustrate the value of a PowerShell V2 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4911"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4911\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}