{"id":1439,"date":"2020-02-14T17:43:45","date_gmt":"2020-02-14T15:43:45","guid":{"rendered":"https:\/\/www.provisior.nl\/techblog\/?p=1439"},"modified":"2020-02-14T17:43:45","modified_gmt":"2020-02-14T15:43:45","slug":"the-power-of-splatting-in-powershell","status":"publish","type":"post","link":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/","title":{"rendered":"The power of splatting in PowerShell"},"content":{"rendered":"<p>While working on a PowerShell script to add users in Azure AD recently, I walked into a situation you will sometimes have as a PowerShell scripter. You need to call some command, but given the input the parameters to supply differs. You could write a couple of &#8220;if this-input then this-call-with-these-params&#8221; statements, but this would certainly not be labeled as &#8220;clean code&#8221;. <\/p>\n<p>Splatting is a perfect solution for this! If you don&#8217;t know what splatting is, you can read all about it <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_splatting?view=powershell-5.1\" rel=\"noopener noreferrer\" target=\"_blank\">here<\/a>. In short:<\/p>\n<blockquote><p>Splatting is a method of passing a collection of parameter values to a command as unit.<\/p><\/blockquote>\n<p>If you look at the &#8220;New-AzureADUser&#8221; command, you can see it has <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/azuread\/new-azureaduser?view=azureadps-2.0\" rel=\"noopener noreferrer\" target=\"_blank\">a pretty long list of parameters<\/a>. It really depends on the input what to pass as parameters. Do we need to set the user&#8217;s mobile phone number or address? By setting up a hashtable of parameters to pass, you can easily leave out cetain parameters based on input. <\/p>\n<pre><code>#Set up the parameters that will be passed all the time\r\n$parameters = @{\r\n    GivenName = $firstName \r\n    Surname = $lastName\r\n    DisplayName = $displayName\r\n    PasswordProfile = $PasswordProfile\r\n    UserPrincipalName = \"$($userid)@mycompany.com\"\r\n    AccountEnabled = $true\r\n    Department = $departmentName\r\n    CompanyName = \"My company\" \r\n    Country = \"NL\"\r\n}\r\n\r\n#Add additional parameters if needed\r\nif (![string]::IsNullOrEmpty($jobTitle)) {\r\n    $parameters.Add(\"JobTitle\", $jobTitle)\r\n}\r\nif (![string]::IsNullOrEmpty($street)) {\r\n    $parameters.Add(\"StreetAddress\", \"$street $houseNo\".Trim())\r\n}\r\nif (![string]::IsNullOrEmpty($postalCode)) {\r\n    $parameters.Add(\"PostalCode\", $postalCode)\r\n}\r\n#etc.<\/code><\/pre>\n<p>In this example I first create a hash table with the parameters I&#8217;d like to add in any case and afterwards I add additional parameters where needed. Last thing to do is passing the hash table to the command like this (Note the @ instead of $): <\/p>\n<pre><code>$newUser = New-AzureADUser @parameters<\/code><\/pre>\n<p>A positive side effect is that the code is much easier to read than providing a long list of parameters to a command, often divided over multiple lines. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on a PowerShell script to add users in Azure AD recently, I walked into a situation you will sometimes have as a PowerShell scripter. You need to call some command, but given the input the parameters to supply differs. You could write a couple of &#8220;if this-input then<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[92,113,97],"class_list":["post-1439","post","type-post","status-publish","format-standard","hentry","category-provisior","tag-automation","tag-azure","tag-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The power of splatting in PowerShell - Provisior Tech Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The power of splatting in PowerShell - Provisior Tech Blog\" \/>\n<meta property=\"og:description\" content=\"While working on a PowerShell script to add users in Azure AD recently, I walked into a situation you will sometimes have as a PowerShell scripter. You need to call some command, but given the input the parameters to supply differs. You could write a couple of &#8220;if this-input then\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Provisior Tech Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-14T15:43:45+00:00\" \/>\n<meta name=\"author\" content=\"Stefan Krul-Donkersloot\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Krul-Donkersloot\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/\"},\"author\":{\"name\":\"Stefan Krul-Donkersloot\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/#\\\/schema\\\/person\\\/a43e8689cceb46a716db67f06fee6877\"},\"headline\":\"The power of splatting in PowerShell\",\"datePublished\":\"2020-02-14T15:43:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/\"},\"wordCount\":240,\"commentCount\":0,\"keywords\":[\"Automation\",\"Azure\",\"PowerShell\"],\"articleSection\":[\"Provisior\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/\",\"url\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/\",\"name\":\"The power of splatting in PowerShell - Provisior Tech Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/#website\"},\"datePublished\":\"2020-02-14T15:43:45+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/#\\\/schema\\\/person\\\/a43e8689cceb46a716db67f06fee6877\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/2020\\\/02\\\/14\\\/the-power-of-splatting-in-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The power of splatting in PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/#website\",\"url\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/\",\"name\":\"Provisior Tech Blog\",\"description\":\"Blogs to help you get the best out of Provisior, the leading Self-service and Automation platform.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/#\\\/schema\\\/person\\\/a43e8689cceb46a716db67f06fee6877\",\"name\":\"Stefan Krul-Donkersloot\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g\",\"caption\":\"Stefan Krul-Donkersloot\"},\"sameAs\":[\"https:\\\/\\\/www.provisior.com\"],\"url\":\"https:\\\/\\\/www.provisior.nl\\\/techblog\\\/author\\\/stefanprovisior\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The power of splatting in PowerShell - Provisior Tech Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/","og_locale":"en_US","og_type":"article","og_title":"The power of splatting in PowerShell - Provisior Tech Blog","og_description":"While working on a PowerShell script to add users in Azure AD recently, I walked into a situation you will sometimes have as a PowerShell scripter. You need to call some command, but given the input the parameters to supply differs. You could write a couple of &#8220;if this-input then","og_url":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/","og_site_name":"Provisior Tech Blog","article_published_time":"2020-02-14T15:43:45+00:00","author":"Stefan Krul-Donkersloot","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Krul-Donkersloot","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/#article","isPartOf":{"@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/"},"author":{"name":"Stefan Krul-Donkersloot","@id":"https:\/\/www.provisior.nl\/techblog\/#\/schema\/person\/a43e8689cceb46a716db67f06fee6877"},"headline":"The power of splatting in PowerShell","datePublished":"2020-02-14T15:43:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/"},"wordCount":240,"commentCount":0,"keywords":["Automation","Azure","PowerShell"],"articleSection":["Provisior"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/","url":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/","name":"The power of splatting in PowerShell - Provisior Tech Blog","isPartOf":{"@id":"https:\/\/www.provisior.nl\/techblog\/#website"},"datePublished":"2020-02-14T15:43:45+00:00","author":{"@id":"https:\/\/www.provisior.nl\/techblog\/#\/schema\/person\/a43e8689cceb46a716db67f06fee6877"},"breadcrumb":{"@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.provisior.nl\/techblog\/2020\/02\/14\/the-power-of-splatting-in-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.provisior.nl\/techblog\/"},{"@type":"ListItem","position":2,"name":"The power of splatting in PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/www.provisior.nl\/techblog\/#website","url":"https:\/\/www.provisior.nl\/techblog\/","name":"Provisior Tech Blog","description":"Blogs to help you get the best out of Provisior, the leading Self-service and Automation platform.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.provisior.nl\/techblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.provisior.nl\/techblog\/#\/schema\/person\/a43e8689cceb46a716db67f06fee6877","name":"Stefan Krul-Donkersloot","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1b2f0b419a9fee792d6a39c1a7bc92b84a848dc7859123a556058736b6481ea7?s=96&d=mm&r=g","caption":"Stefan Krul-Donkersloot"},"sameAs":["https:\/\/www.provisior.com"],"url":"https:\/\/www.provisior.nl\/techblog\/author\/stefanprovisior\/"}]}},"_links":{"self":[{"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/posts\/1439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/comments?post=1439"}],"version-history":[{"count":4,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/posts\/1439\/revisions"}],"predecessor-version":[{"id":1443,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/posts\/1439\/revisions\/1443"}],"wp:attachment":[{"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/media?parent=1439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/categories?post=1439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.provisior.nl\/techblog\/wp-json\/wp\/v2\/tags?post=1439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}