Link to home
Start Free TrialLog in
Avatar of JJENSEN3
JJENSEN3

asked on

Reading and Parsing Data From a File in PowerShell

I am using Powershell to try and read in a delimited file and and parse out the information so I can use it for processing.
I have attached a sample of the file that I would be reading, and am not sure how to parse it out into something usable in Powershell.
The first row of the file are headers and the second row are the corresponding data for those headers. What is the best way to handle this data?

For example - I am trying to assign the "Salesman" corresponding value in row 2 to a variable in my Powershell script. Any suggestions on how to accomplish this.
Example.txt
Avatar of FOX
FOX
Flag of United States of America image

Jensen
Please upload the attachment...it is not here
Avatar of JJENSEN3
JJENSEN3

ASKER

Oops! Sorry about that. It should be attached now.
Based on your description, the Import-Csv cmdlet should fit your needs.  You can specify what delimiter to use when reading the file (default is a comma).  Typically you will pipe an Import-Csv command to a ForEach-Object command to loop through all the objects emitted by Import-Csv.
Example:
Import-Csv file.csv -Delimiter "|" | ForEach-Object {
    # do something.  $_ is the current object in the loop
    # you can reference specific properties of the object like $_.Salesman
    $_
}

Open in new window


I'd recommend reading the built-in help for the cmdlet with examples.
Jensen,
Based on the limited information you can add whatever is under the Salesman column using $_.Salesman.  If you provide a sample of your script we can be better able to help you.

Are the DealerIDs the samaccountnames in your environment and Salesman is one of your properties you can do the below command
Example if you want to set all the users with DealerID to whatever is in Salesman

Import-csv 'c:\filelocation\yourfilename.csv' |%{Set-ADUser $_.DealerID -Salesman $_.Salesman}
I don't have a script written at this point. I am trying to develop one. To clarifiy, in the attached file the first row:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name

are the headers/field names for the data in the second row:
HI||||jsmith||ABC REMODELING|

So what I am trying to do is assign the second row of data to the header tags in the first row.

Example:

HT = HI
Dealer ID= <blank>
Comment = <blank>
Contact Name = <blank>
Salesman = jsmith
Dealer = <blank>
Company name = ABC REMODELING

At that point I can work with each individual variable in the script I am building.
I hope this makes sense. I am not a strong powershell user, so don't be afraid to break it down. I won't be offended. :)
@JJENSEN3 - Did you try what I wrote?
footech -  Thank you. Yes. I did, and it worked - for the most part. Just a couple of follow up questions:

1. The file that I am working with has "address" more than once in the header, so I am getting an error:
Import-Csv : The member "Address" is already present.
Is there a way to prevent this error without manually updating the headers to address1, address2, etc...?

2. There may be multiple rows of data, beyond just the second row in my example. Is there a way to read only the second row and ignore the rest of the rows of data in the csv file?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Footech - Great examples! Thanks for walking me through it. Things are working great.