Comments on: To Splat or Not to Splat, That’s the Question https://office365itpros.com/2024/06/12/splatting-powershell/?utm_source=rss&utm_medium=rss&utm_campaign=splatting-powershell Mastering Office 365 and Microsoft 365 Tue, 18 Jun 2024 17:36:20 +0000 hourly 1 By: Tony Redmond https://office365itpros.com/2024/06/12/splatting-powershell/#comment-12460 Tue, 18 Jun 2024 17:36:20 +0000 https://office365itpros.com/?p=65119#comment-12460 In reply to rjmnu.

It might just be that UserId is a mandatory parameter… Does the same error occur for other parameters?

I did this:

$Params = @{}
$Params.Add(“JobTitle”, “Road Sweeper”)
$Params.Add(“OfficeLocation”, “Paris”)
Update-MgUser -UserId Rene.Artois@Office365itpros.com -Office “Nouvon” -JobTitle “Chief” @Params

Get-MgUser -userid Rene.Artois@Office365itpros.com -Property Id, OfficeLocation, JobTitle | Select OfficeLocation, JobTitle

OfficeLocation JobTitle
————– ——–
Nouvon Chief

As you can see, the explicit parameters took precedence over those in the hash table. But this is PowerShell 7.4.2.

Who knows what might happen elsewhere…

]]>
By: rjmnu https://office365itpros.com/2024/06/12/splatting-powershell/#comment-12459 Tue, 18 Jun 2024 17:23:31 +0000 https://office365itpros.com/?p=65119#comment-12459 Another great article, Tony! I’m a huge fan of splatting and generally avoid using backticks except in rare cases. I tend to start typing commands the “traditional” way with parameters all on the same line to leverage tab completion/IntelliSense. I then let VS Code convert those that have become unwieldy to use splatting, as described here:

https://mikefrobbins.com/2023/10/19/automatically-convert-a-powershell-command-to-use-splatting/

One thing I’ve noticed is that the following doesn’t seem to be true for me in PowerShell version 5.1:

“PowerShell applies the values for the parameters in the hash table except where a parameter value is explicitly passed.”

If I run this (somewhat silly) example:

$getMgUserSplat = @{
UserId = ‘user1@company.com’
Property = ‘Id’, ‘UserPrincipalName’, ‘mySite’
}
$user1 = Get-MgUser @getMgUserSplat
$user2 = Get-MgUser @getMgUserSplat -UserId ‘user2@company.com’

I get the following error on that second Get-MgUser:

Get-MgUser : Cannot bind parameter because parameter ‘UserId’ is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, “-parameter value1,value2,value3”.

When this comes up, I just replace the value in the hash table before running the second instance of the command:

$user1 = Get-MgUser @getMgUserSplat
$getMgUserSplat.UserId = ‘user2@company.com’
$user2 = Get-MgUser @getMgUserSplat

or, limit the hash table to only the “static” parameters and pass the variable parameters explicitly:

$getMgUserSplat = @{
Property = ‘Id’, ‘UserPrincipalName’, ‘mySite’
}
$user1 = Get-MgUser @getMgUserSplat -UserId ‘user1@company.com’
$user2 = Get-MgUser @getMgUserSplat -UserId ‘user2@company.com’

]]>
By: Tony Redmond https://office365itpros.com/2024/06/12/splatting-powershell/#comment-12445 Sun, 16 Jun 2024 18:06:18 +0000 https://office365itpros.com/?p=65119#comment-12445 In reply to Vic.

I know. But I like constructing hash tables in this manner. It just seems more logical and easier to do when building hash tables programmatically.

]]>
By: Vic https://office365itpros.com/2024/06/12/splatting-powershell/#comment-12443 Sat, 15 Jun 2024 13:00:14 +0000 https://office365itpros.com/?p=65119#comment-12443 Instead of creating an empty hashtable object and adding elements one at a time, you can create just a hashtable:

$Body = @{
Office = “Galway”
Department = “Business Development”
}

Update-MgUser -UserId $UserId @Body

]]>