Hello @Ed7
I would use the Compare-Object: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2&viewFallbackFrom=powershell-6
For example I have this process for disabled users in my AD:
TEST_AD.csv - this is my csv file export from AD from all my 'active' users. (System of Record).
TEST_APP.csv - this is my csv file from the application.
TEST_DISABLED_APP_USERS.csv - this is my csv file that I would upload to the application to disable any users not found in the TEST_AD.csv but are found in the TEST_APP.csv file.
$ADCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_AD.csv"
$APPCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_APP.csv"
#for the compare I chose the "Email" header in the CSV file as my 'primary key'. Something you know will exist to identify items (users) in each file that must exist in both files.
$compareCSV = Compare-Object $ADCSV $APPCSV -Property Email -IncludeEqual -PassThru
$output = ForEach ($item in $compareCSV) {
If ($item.SideIndicator -eq "=>") {
#this item (aka User) exists in the 'difference set' (TEST_APP.csv file) so we'll need to add them to our export CSV.
[pscustomobject]@{
#needed to create our output csv file.
#first column is the export CSV headers, second (after '=') is the values/data.
#remove any unnecessary headers/values that the upload file doesn't need.
"Employee ID" = $item.'Employee ID'
"First Name" = $item.'First Name'
"Last Name" = $item.'Last Name'
"Display Name" = $item.'Display Name'
"Telephone" = $item.Telephone
"Email" = $item.Email
"Street" = $item.Street
"City" = $item.City
"State" = $item.State
"Zip Code" = $item.'Zip Code'
"Home" = $item.Home
"Mobile" = $item.Mobile
"Fax" = $item.Fax
"Job Title" = $item.'Job Title'
"Department" = $item.Department
"Company" = $item.Company
"Status" = "Disabled" #this is static and should remain. Or set to whatever value you need for our application to disable the user.
}
}
}
$output | Export-CSV -Path "C:\temp\TestCSV\TEST_DISABLED_APP_USERS.CSV" -NoTypeInformation
Hope this helps with your query,
---------
--If the reply is helpful, please Upvote and Accept as answer--