Questions tagged [parameter-splatting]

A PowerShell technique for passing parameter values via hash tables or arrays rather than individually.

See Get-Help about_splatting.

23 questions
12
votes
2 answers

How to pass a switch parameter as a variable / via splatting in PowerShell?

If you have multiple parameters which require a value when calling a command or a script, I know you can pass it like this: $parameters = @{ name = "John" last_name = "Doe" } But if the command or script actually just expect -T to indicate…
Mason
  • 1,007
  • 1
  • 13
  • 31
6
votes
1 answer

Splatting in C#

In PowerShell you can create a hashtable and add this hashtable to your function with an @, which is splatting in PowerShell. $dict = @{param1 = 'test'; param2 = 12} Get-Info @dict Can one pass a dictionary as a collection of parameters to a…
Alex_P
  • 2,580
  • 3
  • 22
  • 37
5
votes
2 answers

How to achieve @args splatting in an advanced function in Powershell?

Consider the following simple function: function Write-HostIfNotVerbose() { if ($VerbosePreference -eq 'SilentlyContinue') { Write-Host @args } } And it works fine: Now I want to make it an advanced function, because I want it…
mark
  • 59,016
  • 79
  • 296
  • 580
5
votes
2 answers

Use a variable in PowerShell to pass multiple arguments to an external program

I downloaded the npm package for merge junit reports - https://www.npmjs.com/package/junit-merge. The problem is that I have multiple files to merge and I am trying to use string variable to hold file names to merge. When I write the script myslef…
miechooy
  • 3,178
  • 12
  • 34
  • 59
4
votes
2 answers

Implement Decorator Pattern in Powershell

I have my custom function f which runs some stuff then calls a predefined function Invoke-WebRequest I want to make f accepts all the arguments that Invoke-WebRequest does, and pass those arguments to Invoke-WebRequest later. f --UseBasicParsing…
2
votes
2 answers

Powershell splatting: pass ErrorAction = Ignore in hash table

Here's a script to list directories / files passed on the command line -- recursively or not: param( [switch] $r ) @gci_args = @{ Recurse = $r ErrorAction = Ignore } $args | gci @gci_args Now this does not work because Ignore is interpreted as…
usretc
  • 723
  • 4
  • 9
2
votes
3 answers

Splatting variable that start with a space

Is there a way to continue using the name that begins with a space and splat that? So this of course works: $Splat = @{ name = 'chrome' fileversioninfo = $true } (Get-Process @Splat)[0] For me it returns: ProductVersion FileVersion …
Mike
  • 21
  • 2
2
votes
1 answer

Splatting doesn't work with parameter -Filter

I have this long line I want to make easier to read: $Mail = "stantastic@example.com" Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress…
StanTastic
  • 311
  • 3
  • 18
1
vote
2 answers

Using Splat in Invoke-Command AND passing arguments relevant to the machine

I'm using Invoke-Command, but this question can be relevant to any command using splat. I essentially want to pass two sets of variables that will be useful in the splat command, but I'm not sure how I can do this. In the code below, the…
1
vote
0 answers

What is the difference between using @ and $ in front of variable in powershell?

I tried to pass the variables to understand the real meaning of the @ and $ in the powershell as : PS C:\Windows\system32> Write-Host $str string PS C:\Windows\system32> Write-Host @str s t r i n g and when we create an hash table like PS…
Sanjay V
  • 11
  • 1
1
vote
2 answers

Parameter splatting with values read from a JSON config file

Using Splatting Hashtable for to set parameters and works well. I am passing to a function as New-ADUser @param, the values that are set in splatting will be created in Active Directoy . However, my script will be running somewhere else. The Json…
Ashlyne B
  • 11
  • 3
1
vote
1 answer

Powershell Splatting Object Attribute (Typeof System.Collections.Hashtable)

Going to give an example to make it clearer what I want to do $AzLogin = @{ Subscription = [string] 'SubscriptionID'; Tenant = [string] 'tenantID'; Credential = [System.Management.Automation.PSCredential] $credsServicePrincipal; …
0
votes
1 answer

PowerShell: How can I pass a nested hash table to my function that accepts 3 parameters?

I created this function but I don't know how to make it work. here is the code: function ProgramRegistry { param ( [Parameter(Mandatory=$false)][HashTable]$HashTable, [Parameter(Mandatory=$false)][String]$AlertPath, …
user20682592
0
votes
1 answer

PowerShell 7.3 breaks invoking exe with params

After an upgrade from PowerShell v. 7.2.7 to v. 7.3, invoking executables with parameters started to fail consistently with various errors. It looks like the parameters are not passed in correctly anymore. An example (some params removed for…
Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42
0
votes
1 answer

Powershell Data Malformed with Splatting

I am using a custom function that imports data using an API. The function requires several different parameters, which I am currently defining with one $Params hashtable. From there I am adding other parameters that may or may not be added based on…
Andrew Draper
  • 101
  • 1
  • 9
1
2