如何更改由 hashstables Powershell 形成的输出

How to change an output formed by hashstables Powershell

我将以下查询值存储在一个变量中:

$unquotedPaths = Get-WmiObject -Class Win32_Service | Select-Object -Property Name,DisplayName,PathName,StartMode | Select-String "auto"

当我打印该变量时问题就开始了,因为该变量从查询中获取一个由哈希表形成的对象,如以下输出所示:

PS C:\Users\pc> Get-WmiObject -Class Win32_Service | Select-Object -Property Name,DisplayName,PathName,StartMode | Select-String "auto"

    @{Name=AGMService; DisplayName=Adobe Genuine Monitor Service; PathName="C:\Program Files (x86)\Common Files\Adobe\AdobeGCClient\AGMService.exe"; StartMode=Auto}
    @{Name=AGSService; DisplayName=Adobe Genuine Software Integrity Service; PathName="C:\Program Files (x86)\Common Files\Adobe\AdobeGCClient\AGSService.exe"; StartMode=Auto}
    @{Name=asComSvc; DisplayName=ASUS Com Service; PathName=C:\Program Files (x86)\ASUS\AXSP.01.02\atkexComSvc.exe; StartMode=Auto}
    @{Name=AudioEndpointBuilder; DisplayName=Compilador de extremo de audio de Windows; PathName=C:\WINDOWS\System32\svchost.exe -k LocalSystemNetworkRestricted -p; StartMode=Auto}

我怎样才能像这样得到和输出:

          Name      DisplayName         PathName      Startmode
      ----------   -------------       ------------   ------------
   ExampleName      ExampleDisplayName  C:\Example    Auto

Select-String is meant to search and match patterns among strings and files, If you need to filter an object you can use Where-Object:

$unquotedPaths = Get-WmiObject -Class Win32_Service |
Where-Object StartMode -EQ Auto |
Select-Object -Property Name,DisplayName,PathName,StartMode

如果过滤需要更复杂的逻辑,您需要将比较语句更改为脚本块,例如:

$unquotedPaths = Get-WmiObject -Class Win32_Service | Where-Object {
    $_.StartMode -eq 'Auto' -and $_.State -eq 'Running'
} | Select-Object -Property Name,DisplayName,PathName,StartMode