0

Wrote this script to combine info from two CSVs into one CSV, however, I noticed that if there is not a match in the if statement, then no record is written. How do I get it to write a record even though there is no match for: if ($hr.'Last Name' -eq $user.'Last Name' -And $hr.'First Name' -eq $user.'First Name' )

In other words, if there is not a match, I would like it to write the original record from the combined.csv file.

$hrUsers = Import-Csv -Path "C:\temp\HR.csv"
$combinedUsers = Import-Csv "C:\temp\combined.csv"


foreach ($user in $combinedUsers){
    $matched = $false
    foreach ($hr in $hrUsers){
        $obj = "" | select "First Name","Last Name","Enrollment Date","Due Date","Status","Email Address","Manager", "Company"
        if ($hr.'Last Name' -eq $user.'Last Name' -And $hr.'First Name' -eq $user.'First Name' ) {
            $matched = $true
            $obj.'First Name' = $user.'First Name'
            $obj.'Last Name' = $user.'Last Name'
            $obj.'Enrollment Date' = $user.'Enrollment Date'
            $obj.'Due Date' = $user.'Due Date'
            $obj.'Status' = $user.'Status'
            $obj.'Email Address' = $user.'Email Address'
            $obj.'Manager' = $user.'Manager'
            $obj.'Company' = $hr.'Company'
            $obj | Export-Csv -Path "C:\temp\experiment2.csv" -Append -NoTypeInformation
        }        
    }
}
  • 1
    Place `| Export-Csv -Path "C:\temp\experiment2.csv"` (without append) outside the loop (at the end of the pipeline). Than, on your `if` do also an `else` with what you like to output when there is not match. Anyways, if you do not reinvent the wheel you might use this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `$combinedUsers |FullJoin $hrUsers -on "First Name","Last Name"` – iRon Sep 15 '22 at 09:04
  • As aside, wouldn't checking on the `Email Address` property be simpler as compared to the Last and First name properties? And... When you find a user in the HR list, you output all properties from the `$combinedUsers` file, except the 'Company'. That would mean you only have to output whatever is in `$combinedUsers`, only change the Company property when a match is found? – Theo Sep 15 '22 at 12:40
  • Unfortunately, the HR.csv does not contain a field for email so I can't key off of that. I'll try the Join-Object script @iRon and report back – Section8Network Sep 15 '22 at 18:20
  • It is a better practice to build your objects like this: `$obj = [PSCustomObject]@{ 'First Name' = $user.'First Name'; ... ` and just asign an empty string to your `email` in case it is only `$hr`. – iRon Sep 16 '22 at 05:57
  • 1
    @iRon, thank you. I ended up using your "Join-Object" script and a FullJoin to combine the tables. Much appreciated! – Section8Network Sep 19 '22 at 16:09

0 Answers0