Parsing and Extracting Data From A Text File In PowerShell

I have a Powershell script that reads the full content of an external data file. The file contains one line of header fields and the second line contains the associated data to those fields. My question is- how would I extract one of the fields in that file, in this case the "PO Number" field, and assign it to a variable in powershell? Below is the example of the file content and the command in Powershell to read the file.

File Data:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|

HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Initial Read Command:
$content = [IO.File]::ReadAllText($directorypath + "\Sample File.txt")
JJENSEN3Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
There's always the straight-forward way: count and split; the field in question is the 17th, so you need the zero-based index 16:
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$poNumber = (Get-Content -Path $inFile | Select-Object -Index 1).Split('|')[16]

Open in new window

Or the more versatile one, where you make the data csv compatible (multiple "Address" columns, empty last column), and then import it as csv with access to the properties:
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$i = $j = 1
$header = (Get-Content -Path $inFile -TotalCount 1).Split('|') | ForEach-Object {If ($_ -eq 'Address') {"$($_)$(($i++))"} ElseIf (-not $_) {"null$(($j++))"} Else {$_}}
$csv = Get-Content -Path $inFile | Select-Object -Skip 1 | ConvertFrom-Csv -Header $header -Delimiter '|'
$poNumber = $csv.'PO Number'

Open in new window

0
JJENSEN3Author Commented:
I am trying your "straight-forward" suggestion, but it is returning a null value.
0
oBdACommented:
I was testing with this:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|
HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Open in new window


Or is there an actual empty second line in the data?
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|

HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Open in new window

0
oBdACommented:
Assuming there is one (or more) empty lines after the header, straight-forward would be
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$poNumber =  (Get-Content -Path $inFile | Where-Object {$_} | Select-Object -Index 1).Split('|')[16]

Open in new window

ConvertTo-Csv doesn't care about empty lines.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JJENSEN3Author Commented:
Thanks so much for the clarification. There was a null second line. Your revised code took care of the issue.
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.