none
Powershell Match 2 files RRS feed

  • Question

  • Greeting,

    I have 2 files available.

    1st "FromAD.csv" is the whole users list from AD as below:

    "EmployeeID","SamAccountName","emailaddress"
    ,"Administrator",
    ,"Guest",
    ,"DefaultAccount",
    ,"krbtgt",
    "0000000033","isomount","isomount@v715.xu"
    "0000088888","admin1","admin1@v715.xu"
    ,"dgadmin",
    ,"fdadmin",
    ,"scope",
    .....


    only 2 line as below:
    "0000000033","isomount","isomount@v715.xu"
    "0000088888","admin1","admin1@v715.xu"

    have the EmployeeID, the other do not have it.

    The 2rd file "Looking.csv" is the EmployeeID that try to found as below:

    EmployeeID
    0000000033
    123456
    789167
    753145
    0000088888
    .....

    What I am looking for is the 3rd csv file will be create, like below,

    "0000000033","isomount","isomount@v715.xu"
    "0000088888","admin1","admin1@v715.xu"

    Because that were only 2 lines matched,

    Any Idea how to start them? the real records for both 2 files maybe over 10000

    Thanks and Best Regards,
    newbie in Powershell

    Friday, April 27, 2018 3:57 PM

Answers

  • $looking = Import-Csv looking.Csv | select -Expand EmployeeID
    Import-Csv FromAd.CSv | Where{$_.EmployeeID -in $looking }
    


    \_(ツ)_/

    • Marked as answer by userausera Friday, April 27, 2018 6:13 PM
    Friday, April 27, 2018 6:01 PM

All replies

  • Example (assuming the "FromAD.csv" file is well-formed and doesn't contain errors):


    Import-Csv FromAD.csv | Where-Object { $_.EmployeeID } | Export-Csv outputfile.csv -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    Friday, April 27, 2018 4:25 PM
    Moderator
  • Thanks,

    But I did not see any match operate.

    Import-Csv FromAD.csv | Where-Object { $_.EmployeeID } | Export-Csv outputfile.csv -NoTypeInformation

    My idea is use "Looking.csv" to match "fromad.csv"

    once match, then export to the 3rd file, if NOT then skip it to next record

    Thanks

    userausera



    • Edited by userausera Friday, April 27, 2018 4:38 PM
    Friday, April 27, 2018 4:36 PM
  • What use is "looking.csv" in this context?

    -- Bill Stewart [Bill_Stewart]

    Friday, April 27, 2018 5:26 PM
    Moderator
  • Hi Bill, again thanks for your feedback

    What I am looking for is the 3rd csv file will be create, like below,

    "0000000033","isomount","isomount@v715.xu"
    "0000088888","admin1","admin1@v715.xu"

    Because that were only 2 lines matched,

    userausera

    Friday, April 27, 2018 5:58 PM
  • $looking = Import-Csv looking.Csv | select -Expand EmployeeID
    Import-Csv FromAd.CSv | Where{$_.EmployeeID -in $looking }
    


    \_(ツ)_/

    • Marked as answer by userausera Friday, April 27, 2018 6:13 PM
    Friday, April 27, 2018 6:01 PM
  • thanks Jrv,

    it works, I saw result on the console, how about the result to the csv file?

    thanks

    userausera

    Friday, April 27, 2018 6:12 PM
  • Before proceeding with any more questions you need to learn basic PowerShell.  Bill's example shows how to export.


    \_(ツ)_/

    Friday, April 27, 2018 6:18 PM
  • Thanks!
    Friday, April 27, 2018 6:43 PM