thepace
thepace

Reputation: 2221

Where-Object with Get-Content not working

I have a file, say a.txt with the following content:

STATUS
-----
Pending

I am running the following query:

Get-Content a.txt | Where-Object Status -Match 'Pending'

The above query returns nothing.

Adding to my requirement:

Note: Where-Object Status -Match 'Pending' cannot be changed to Select-String because the function is used by many sources.

FIX:

$contentOfFile = ConvertFrom-Csv (Get-Content a.txt)
$contentOfFile | Where-Object Status -Match 'Pending'

Upvotes: 0

Views: 1972

Answers (1)

Vivek Kumar Singh
Vivek Kumar Singh

Reputation: 3350

When you use Get-Content , the STATUS in your text file is stored as a plain text and not as a property. If you intend to use Get-Content, you can use Select-String cmdlet like

Get-Content a.txt | Select-String -pattern 'Pending'

But if you want to get the output as you desire, you can use the Import-Csv. What it will do is, it will make your STATUS as a -Header property and then you can use the where-object as you have used earlier. After all, a csv file is a text file, just comma-separated. Below works for me:-

Import-Csv a.txt | Where-Object Status -Match 'Pending'

Upvotes: 3

Related Questions