Reputation: 2221
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:
I need to get the content in a variable, like
$contentOfFile = Get-Content a.txt
This variable is passed to a function that verifies if it has the match
$contentOfFile | **Where-Object Status -Match 'Pending'**
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
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