# Update record file 2 based upon info from file 1

**URL:** <https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176>\
**Category:** PowerShell Help\
**Created:** [April 20, 2023, 7:21am UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176 "2023-04-20T07:21:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [April 20, 2023, 7:21am UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/1 "2023-04-20T07:21:07Z")

</div>

HI  
during the offboarding of users I get the original request stating that user with employeeID 1 is leaving on 30-04-2023, this is stored in file 1.it might very well happen that after a little while I get an update that user with employeeID 1 is now leaving on 30-05-2023. this is stored in file 2  
What would be the best scenario to update file 1 lastworkdate with the lastworkdate from file 2.  
I was thinking of using the foreach or even get file content compare and replace.  
The foreach option will get the user where the lastworkdate is different but I would now need to replace it with the updated value in in file 1.

```auto
$update = import-csv "C:\Users\onboarding_it.gen\Documents\temp\Updateleaver_db.csv"
$db = import-csv "C:\Users\onboarding_it.gen\Documents\temp\offboarding_db.csv"

$test = foreach($UpdateRow in $Update){
    $UpdatePn = $UpdateRow.Pn
    $UpdateLastWorkdate = $UpdateRow.Lastworkdate
    if($UpdatePn -in $Db.pn -and $UpdateLastWorkdate -ne $db.Lastworkdate){
        [PSCustomObject]@{
            PN = $UpdateRow.Pn 
            Name = $UpdateRow.Name
            samaccountname = $UpdateRow.samaccountname
            Email = $UpdateRow.Email
            Manager = $UpdateRow.Manager
            OfficeLocation = $UpdateRow.OfficeLocation
            Lastworkdate = $UpdateLastWorkdate
            RequestID = $UpdateRow.RequestID
            MsgID = $UpdateRow.msgID
            Treated = $UpdateRow.treated
            treatmentdate = $today
            }
    }

}
$test |export-csv "C:\Users\onboarding_it.gen\Documents\temp\offboarding_db_chk.csv" -NoTypeInformation 

```

I did put the differences in a seperate file at the moment but need to incorporate the updated value in the db file

---

<div class="post-metadata">

**Author:** ![ZaMotH](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@ZaMotH](https://forums.powershell.org/u/ZaMotH)\
**Post date:** [April 20, 2023, 2:36pm UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/2 "2023-04-20T14:36:43Z")

</div>

Hello,  
Firstly, I will use hashtables as comparing is much more efficient  
Then I will loop on my target data `$db` because … this is the data we want to update

```auto
$db = import-csv "C:\Users\onboarding_it.gen\Documents\temp\offboarding_db.csv"
$update = import-csv "C:\Users\onboarding_it.gen\Documents\temp\Updateleaver_db.csv" | -Property pn -AsHashTable

$test = foreach ($row in $db){
    $Target = $update[$row.pn]
    if ($Target -and $Target.Lastworkdate -notlike $row.Lastworkdate) {
      write-host Updating $row.pn to $Target.Lastworkdate
      $row.Lastworkdate = $Target.Lastworkdate
    }
}
$db |export-csv "C:\Users\onboarding_it.gen\Documents\temp\offboarding_db_updated.csv" -NoTypeInformation 

```

---

<div class="post-metadata">

**Author:** ![Matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Matt](https://forums.powershell.org/u/Matt)\
**Post date:** [April 20, 2023, 4:35pm UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/3 "2023-04-20T16:35:32Z")

</div>

I know this was not your question and I am certainly not suggestion you do not learn how to do what you are asking about, just offering some food for thought.

Has your organization considered a proper identity management tool? Something like Microsoft Identity Manager (MIM) or similar? These types of tools automate the management of the life cycle of an ID and are ideal for handling changes like you are trying to account for. They do require a lot of work to setup, but they really do pay for them self. There is a lot of repetition in managing IDs, and these tools make life easier by doing that work for you, once configured to your business’s needs.

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [April 21, 2023, 2:36am UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/4 "2023-04-21T02:36:01Z")

</div>

Hi Matt,  
thanks for the info will take a look at that  
best regards  
Paul

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [April 21, 2023, 2:42am UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/5 "2023-04-21T02:42:36Z")

</div>

Hi Zamoth,  
thanks for your reaction, when I try your code I get this error message  
-Property : The term ‘-Property’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.  
please advise  
Paul

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [April 21, 2023, 3:33am UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/6 "2023-04-21T03:33:57Z")

</div>

I’ve modified your code slightly which is now looking like this

```auto
$db = import-csv "C:\Users\onboarding_it.gen\Documents\temp\test\offboarding_db.csv"
$update = import-csv "C:\Users\onboarding_it.gen\Documents\temp\test\Updateleaver_db.csv" | Group-Object PN -AsHashTable

$test = foreach ($row in $db){
    $Target = $update[$row.pn]
    if ($Target -and $Target.Lastworkdate -notlike $row.Lastworkdate) {
      write-host Updating $row.pn to $Target.Lastworkdate
      $row.Lastworkdate = $Target.Lastworkdate
    }
}
$db|export-csv "C:\Users\onboarding_it.gen\Documents\temp\offboarding_db_updated.csv" -NoTypeInformation 

```

now I can see that the if statement is working but it doesn’t write the changes to the db file for some reason  
and shouldn’t $test be piped into the export-csv?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/update-record-file-2-based-upon-info-from-file-1/22176/7 "2024-05-16T20:14:58Z")

</div>


