Splatting did not work on PowerShell v7 but it did in v5.1

Hi everyone,

I was doing some exercises in PowerShell and came up with the following solution:

$params = @{   
            ComputerName=$env:COMPUTERNAME
            PowerShellVersion=$PSVersionTable
            CurrentDate=(Get-Date).ToLongDateString()
}

$params.Add('Name',@('Bits','WinRM'))
$params.Remove('PowershellVersion')
$params.Remove('CurrentDate')

Get-Service @params

When I tried to splat in v7 I keep getting the error:
Get-Service: A parameter cannot be found that matches parameter name 'ComputerName'.
However when I looked at the solution it looked exactly like that, then I tested in v5.1 and worked.

Does anybody know why this does not work in v7 ?

Have you checked the docs for the 5.1 version and the 7.x version of the Get-Service?

The answer is there. Take a look, see if you can see it. Let us know what you find.

Here is a hint, while 7.x has its roots in 5.1, 7.x is not simply the “next version”. They are two different versions of PS. Which means they will not function the same. There are differences in parameters.

3 Likes

Hi Matt,

Thanks for the reply, checking the documentation for both versions is definitely something I missed.

Looking at it now I can see the difference.

While v5.1 has the parameter -ComputerName, v7 does not and that’s why it failed, it can not recognize such parameter.

Also I see another difference is that v7 is meant to work on a computer, so if you need to query multiple ones I think you’d need to use Invoke-Method instead of a parameter name.

Appreciate the help.

1 Like

anytime.

Devil is in the details sometimes; I have also missed small details in the docs.

1 Like