[Solved] Powershell Script to parse selected information from txt file and output to csv


As suggested

  • choose a regular expression that matches your requrements (see regex101.com)
  • iterate the matches and compare the ,matched text
  • generate a [PSCustomObject] for your csv

## Q:\Test\2018\10\17\SO_52857274.ps1
$RE = [RegEx]'^.*?\.(\d{4}):[^"]+"\s*(included with your TV purchase|.*)"$'

$CSV = Select-String '.\my.txt' -Pattern $RE | ForEach-Object {
    IF ($_.Matches.Groups[2].Value -eq 'included with your TV purchase'){
        $Install = $True
    } else {
        $Install = $False
    }
    [PSCustomObject]@{
        Store   = $_.Matches.Groups[1].Value
        Install = $Install
        Date    = (Get-Date).Date
    }
}
$CSV
# $CSV | Export-CSV '.\my.csv' -NoTypeInformation

Sample output:

> Q:\Test\2018\10\17\SO_52857274.ps1

Store Install Date
----- ------- ----
0009     True 2018-10-17 00:00:00
0010     True 2018-10-17 00:00:00
0010    False 2018-10-17 00:00:00

3

solved Powershell Script to parse selected information from txt file and output to csv