Home > Blockchain >  How to change an output formed by hashstables Powershell
How to change an output formed by hashstables Powershell

Time:12-24

I am storing the following query value in a variable:

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

The problem starts when i print that variable becouse the variable takes from the query an object which is formed by hashtables like in this output:

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\1.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}

How i can get and output like this:

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

CodePudding user response:

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

If the filtering required more complex logic you would need to change from Comparison Statement to Script Block, for example:

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