Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from PowerShell 5.1 to 7.2.5 Get-Service -ComputerName doesn't exist anymore, how do I get computer name then?

Tags:

powershell

I'm using Get-Service to show a filtered list and it worked fine in 5.1. This is how it worked in 5.1 for me:

Get-Service -DisplayName $displayName -ComputerName $computers | Sort-Object MachineName | format-table Name,Status,DisplayName,Machinename –autosize 

However, in 7.2.5 -ComputerName is no longer there.

like image 279
Rod Avatar asked Sep 11 '25 19:09

Rod


1 Answers

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.

like image 126
mklement0 Avatar answered Sep 15 '25 15:09

mklement0