powershellpowershell-7.4

Retrieving Username property from Get-Service commands on remote servers thru Powershell 7


In PowerShell 7.0, if I run Get-Service | Select Name, Username | ft I get results like:

Name                                      UserName
----                                      --------
AarSvc_42bd40
AbtCdcSvc                                 LocalSystem
AbtSngSvc                                 LocalSystem
AdobeARMservice                           LocalSystem
AdobeUpdateService                        LocalSystem

Unfortunately Get-Service stopped including the -ComputerName property starting with PowerShell 6.0; I'm trying to run the same command remotely in PowerShell 7.0, but it's "acting funny".

If I run the same command remotely:

Invoke-Command -ComputerName 'remoteServerName' -ScriptBlock { Get-Service | Select Name, UserName | ft }

I get empty Usernames:

Name                                     UserName
----                                     --------
AJRouter
ALG
AppHostSvc
AppIDSvc
Appinfo

Does anyone know why the UserName is missing?


Solution

  • This is because your server is running with Powershell version < 6.0. UserName added to the Get-Service cmdlet result starting from Powershell 6.

    Quoting from the documentation:

    Beginning in PowerShell 6.0, the following properties are added to the ServiceController objects: UserName, Description, DelayedAutoStart, BinaryPathName, and StartupType .

    You can use the Get-CimInstance Cmdlet to query the Win32_Service class in order to get the same result in older versions.

    Get-CimInstance -ClassName Win32_Service | Select-Object -Property Name, startName