Billy Lannan
Billy Lannan

Reputation: 15

Powershell: Only export string values

I have a script that reads a text file, captures specific string values via select-string and then writes to a csv file. The issue I am having is it automatically adds additional columns I do not want:

IgnoreCase LineNumber Line Filename Path Pattern Context Matches

The string values I want are under Line - How do I have it output ONLY the string values I have captured?

Upvotes: 1

Views: 1126

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

Use Select-Object -ExpandProperty to grab just a single property value from the MatchInfo object that Select-String returns:

$MatchedStrings = Get-Item C:\Windows\system32\WindowsPowerShell\v1.0\en-US\about_Foreach.help.txt |select-string "operator"
$MatchedStrings | Select-Object -ExpandProperty Line | Out-File C:\myStrings.txt

If you want to output to a single-column CSV file and retain the Line header, use Select-Object -Property instead:

$MatchedStrings | Select-Object -Property Line | Export-Csv C:\strings.csv -NoTypeInformation

Upvotes: 1

Related Questions