The -ComputerName
parameters on purpose-specific cmdlets such as Get-Service
, Get-Process
and Restart-Computer
only work in Windows PowerShell and aren't available in PowerShell (Core) 7+ anymore, because they are based on .NET Remoting, a form of remoting unrelated to PowerShell that has been declared obsolete and is therefore not part of .NET Core / .NET 5+, which PowerShell (Core) is based on.
Thus, switch to using PowerShell's WinRM-based remoting, where general-purpose remoting cmdlets such as Invoke-Command
cmdlet facilitate execution of arbitrary commands remotely, using a modern, firewall-friendly transport.
However, note that this requires all target computers to be set up for PowerShell remoting first, typically by running Enable-PSRemoting
on the target machine with administrative privileges, though in server editions starting with Windows Server 2012 PowerShell remoting is enabled by default - see the docs.
Once they are, they can also be used with the CIM cmdlets (e.g., Get-CimInstance
), the successors to the obsolete, also Windows PowerShell-only WMI cmdlets (e.g., Get-WmiObject
) - see this answer.
Thus, assuming the target computers are set up for PowerShell remoting, the equivalent of your command is:
Invoke-Command -ComputerName $computers {
Get-Service -DisplayName $using:displayName
} |
Sort-Object PSComputerName |
Format-Table Name, Status, DisplayName, PSComputerName –AutoSize
Note the use of the $using:
scope to refer to the value of a variable from the caller's scope, and the use of the .PSComputerName
property, which PowerShell's remoting infrastructure decorates all output objects with.