{"id":2250,"date":"2014-01-11T00:01:00","date_gmt":"2014-01-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/11\/weekend-scripter-wow-i-didnt-know-you-could-do-that-in-powershell\/"},"modified":"2014-01-11T00:01:00","modified_gmt":"2014-01-11T00:01:00","slug":"weekend-scripter-wow-i-didnt-know-you-could-do-that-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-wow-i-didnt-know-you-could-do-that-in-powershell\/","title":{"rendered":"Weekend Scripter: Wow! I Didn&#8217;t Know You Could Do That in PowerShell!"},"content":{"rendered":"<p><b>Summary<\/b>: Discover the power of the &ldquo;double splat.&rdquo;\nHonorary Scripting Guy, Sean Kearney, here filling in for our good friend, Ed. He&rsquo;s keeping himself inside where it&rsquo;s warm. I&rsquo;m here in Canada typing really fast to keep even warmer! It&rsquo;s 31 degrees below Celsius here in Ottawa, don&rsquo;t cha know?\nI was developing a solution that needed to authenticate against two Active Directory domains, and the client threw in a twist during development (yes, these things happen). During this process, I discovered a cool trick called &ldquo;double splatting.&rdquo;\nIn my script, I first needed to authenticate against two possible Active Directory domains. &ldquo;Well, that&rsquo;s awfully easy,&ldquo; I thought to myself. &ldquo;I&rsquo;ll simply have two Windows PowerShell variables to hold the credentials and the domain controller, and use them as I create users. Too easy.&rdquo;\nYes, that was the original thought. But as the script grew, feature creep from the client came forth. Additional parameters, such as target organizational units were be added to the mix. &nbsp;Multiple situations with more Active Directory cmdlets appeared.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nI found that I was adding the same information over and over and over&hellip;\nSo to resolve that issue, I decided to bite the bullet and use &ldquo;splatting.&rdquo; Splatting is a cool technique in Windows PowerShell where you can define parameters for a cmdlet and its values within an array, and pass that object directly to the cmdlet. The big advantage of splatting (other than avoiding some typing and a readability improvement) is that if there are common parameters that I need to change &ldquo;en-masse&rdquo; in a script, I can edit the array.\nIn my original script, I wrote something like this to query a user:<\/p>\n<p style=\"margin-left:30px\">#Domain Controller<\/p>\n<p style=\"margin-left:30px\">$DC=&rdquo;192.168.1.5&rdquo;\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">#Domain Credentials<\/p>\n<p style=\"margin-left:30px\">$MyDomain=&rsquo;Contoso.Dev&rsquo;<\/p>\n<p style=\"margin-left:30px\">$MyClearTextUsername=&rsquo;ServiceAdminAcct&rsquo;<\/p>\n<p style=\"margin-left:30px\">$MyClearTextPassword=&rsquo;S3cr3tP@ssw0rd!&rsquo;\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">$MyUsernameDomain=$MyDomain+&rsquo;&rsquo;+$MyClearTextUsername\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">$SecurePassword=Convertto-SecureString &ndash;String<\/p>\n<p style=\"margin-left:30px\">$MyClearTextPassword &ndash;AsPlainText &ndash;force\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">$Cred=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword\nThen each time I would try to query a user, I would do this:<\/p>\n<p style=\"margin-left:30px\">REMOVE-ADUser JohnSmith &ndash;credential $Cred &ndash;server $DC\nTo use splatting, first create an array that contains the names of parameters for a cmdlet and their values:<\/p>\n<p style=\"margin-left:30px\">$ADparams=@{&lsquo;credential&rsquo;=$Cred;&rsquo;server&rsquo;=$DC}\nThen pass it directly to the cmdlet:<\/p>\n<p style=\"margin-left:30px\">REMOVE-ADUser JohnSmith @ADparams\nThis now makes flipping the credentials and servers easier. I can also add additional parameters to the same array, and the cmdlets will use them. I was in my glory. Life was good&hellip;until&hellip;\nThe client decided, yes, feature creeeeep!\nThey needed to ensure that they could have a safety switch to test the script&mdash;not just a single cmdlet&mdash;without hurting data.\nWell, in Windows PowerShell, that&rsquo;s easy. That&rsquo;s the <b>&ndash;whatif<\/b> parameter. I can flip cmdlets to automatically default to a <b>whatif<\/b> mode by setting this variable:<\/p>\n<p style=\"margin-left:30px\">$WhatIfPreference=$TRUE\nThis worked like a charm, but every cmdlet (including those for creating logs) warned about what they were going to do without doing it. In my case, I only wanted it to happen for Active Directory cmdlets.\nSoooooo&hellip;scrap that idea. &nbsp;\nThen I stumbled across something really cool. When mucking about, I found that I can splat twice!\nYes, the parser doesn&rsquo;t only work on one splat, you can have multiple in the same cmdlet. In my case, I created a tiny splat for the <b>&ndash;whatif<\/b> parameter by defining one of two possible values.<\/p>\n<p style=\"margin-left:30px\">$Safety=@{&lsquo;whatif&rsquo;=$NULL}<\/p>\n<p style=\"margin-left:30px\">Or<\/p>\n<p style=\"margin-left:30px\">$Safety=@{}\nTo work with those cmdlets, I can pass two splats to the Active Directory cmdlets that need a safety switch, and change the script globally from the top level as a parameter:<\/p>\n<p style=\"margin-left:30px\">REMOVE-ADuser JohnSmith @AdParams @Safety\nThe double splat! An available, built-in feature in Windows PowerShell. It&rsquo;s been sitting there and ready to use the whole darn time!\nWe now return you to your regularly scheduled scripting!\nI invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to <a href=\"http:\/\/blogs.technet.commailto: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.\n<b>Sean Kearney<\/b>, Honorary Scripting Guy and Windows PowerShell MVP&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Discover the power of the &ldquo;double splat.&rdquo; Honorary Scripting Guy, Sean Kearney, here filling in for our good friend, Ed. He&rsquo;s keeping himself inside where it&rsquo;s warm. I&rsquo;m here in Canada typing really fast to keep even warmer! It&rsquo;s 31 degrees below Celsius here in Ottawa, don&rsquo;t cha know? I was developing a solution [&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":[56,154,170,61,45],"class_list":["post-2250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-splatting","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Discover the power of the &ldquo;double splat.&rdquo; Honorary Scripting Guy, Sean Kearney, here filling in for our good friend, Ed. He&rsquo;s keeping himself inside where it&rsquo;s warm. I&rsquo;m here in Canada typing really fast to keep even warmer! It&rsquo;s 31 degrees below Celsius here in Ottawa, don&rsquo;t cha know? I was developing a solution [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2250","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=2250"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2250\/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=2250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}