It often happens that you have many, many parameters in PowerShell. The command then extends over several lines. That doesn’t look pretty. In this article, I will show you how to summarize these parameters and then use them in the command. Let’s get started.
Define Hash Table with Parameter Names and Values
Let’s summarize these parameters in a hash table.
$params = @{
Parameter1 = "Value1"
Parameter2 = "Value2"
Parameter3 = "Value3"
}
Next, we call the command by using our hash table which contains all the parameters and their values.
Invoke-Command @params
That’s it. Mission completed.
But wait, let me show you another example which is more from the real word, also called a real-world-scenario. The code below creates a new Active Directory user using splatting.
$hash=@{
GivenName = 'Florian'
Surname = 'Perfekt'
Name = 'Florian Perfekt'
Enabled = $true
AccountPassword = (ConvertTo-SecureString -AsPlainText '123user!' -Force)
DisplayName = 'Florian Perfekt'
UserPrincipalName = 'florian.perfekt@pagr.loc'
Path = 'OU=Benutzer,DC=pagr,DC=loc'
}
New-ADUser @hash
Hope this was helpful.
Categories: PowerShell, Windows Server




1 reply »