Reputation: 3
I have a txt file that I am reading from.
The file is formatted by ID NUMBER,DEPARTMENT
Example:23942,English
Always 5 digit ID number, department can vary in length.
The first step I was able to do, which was to pull from the first file anyone from the English department.
$x = Get-Content -Path C:\dep.txt | Where-Object {$_.Contains("English")}
This returns "00000,English 11111,English 22222,English......etc)"
However, now I am bit confused. For every ID NUMBER
that gets returned, I need to store them into a separate variable.
Then I need to take those variables (let's just say a,b,c....etc) and compare them to a second text file that is formatted as LASTNAME,FIRSTNAME,ID NUMBER
. Example Alex,Jones,29472
So using the variables (a,b,c,...etc) I need to locate the corresponding ID NUMBER
and find the LASTNAME,FIRSTNAME
that goes with it and write the name to names.txt
I'd appreciate any help I could get with this!
Upvotes: 0
Views: 201
Reputation: 25001
Import-Csv is your friend here. You can simply read your file's contents into a PowerShell object. As an object, you can reference its property values and perform conditional searches.
$deps = Import-Csv c:\dep.txt # Use this if your file already has column headers
# For English department objects
$deps | Where Department -eq 'English'
# For ID Number values for English Departments
($deps | Where Department -eq 'English').'ID Number'
# Let's match department numbers with people
$people = Import-Csv people.csv
$Ids = ($deps | Where Department -eq 'English').'ID Number'
$people | Where 'ID Number' -in $Ids
If your CSV files don't have headers, you can add them to the command with the -Header
parameter:
$deps = Import-Csv c:\dep.txt -Header 'ID Number','Department'
Upvotes: 2