How do select a property in a file when pipelined by get-content?

Hello there, I am a newbie with Powershell.

I have got a file which was printed out to a .txt file from a cmd in PowerShell (eg: Get-Process)

Get-Process | Out-File -FilePath "C:\temp\processes.txt"
After that, I pipeline it via the Get-Content cmdlet and I want to select a column to continuously pipeline to Where-Object cmdlet (eg: Process name). So how can I use $_.Processname ?
Get-Content -Path "C:\temp\processes.txt" | Where-Object {$_.Processname -match "svchost"}
Thank you!

 

Once the output is redirected to a file , it becomes string/plain text. PowerShell pipelines to connect cmdlets are basically to handle objects, so this way it wont be possible. But if you want to store the output as file then Export-CliXml is the hero here. It creates xml file which preserves the object with its type along with the output.

Get-Process | Export-CliXml -Path c:\temp\ps.xml

Import-CliXml -Path c:\temp\ps.xml | Where-Object {$_.Processname -match "svchost"}

Thank you a lot :slight_smile: however, there was a time I saw someone using a txt file which was self-created by him with a header as property and other values below it.

He also pipelines it by get-content and something else after to handle it. Can you consider it? :slight_smile:

 

header as property and other values below it.
It sounds like you are describing a CSV file. See if this is what you're after.

Get-Process | Export-CSV c:\temp\processes.txt -NoType

and then

Import-CSV c:\temp\processes.txt | where processname -match "svchost"

$file = "C:\temp\processes.txt" ; Get-Process | Tee-Object $file | Where-Object {$_.Processname -match "svchost"}

Great !!! That is exactly what I was looking for :smiley:

Thank you,