question

RenuMurali-7202 avatar image
0 Votes"
RenuMurali-7202 asked RenuMurali-7202 commented

power shell script to compare 2 csv and give only inactive users

I have 2 csv files.1st contains all the user info from AD.2nd once contains selected users from AD. I would want to compare these 2 files. I have the script which gives me matching users but i want the non-matching csv as output. Masterlist.csv contains 250 users .I want the inactive users only in mastelist.csv.I have attached my script below any help would be greatly apprectiated.
cls
$OrdersA = Import-CSV -Path C:\Users\renu\Desktop\renu\Masterlist.csv
$OrdersB = Import-CSV -Path C:\Users\renu\Desktop\renu\ALLUserMain30082021.csv
$matchcounter = 0
$start = [system.datetime]::Now
foreach ($order1 in $OrdersA){
$matched = $false
foreach ($order2 in $OrdersB){
$obj = "" | select "e-mailaddress","Last Name","Legal First Name","Anglicized Western First Name","Last/First Legal","Job Title", "Job Location"
if(($order1.'e-mailaddress') -eq $order2.'EmailAddress' ){
$matchCounter++
$matched = $true
$obj.'e-mailaddress' = $order1.'e-mailaddress'
$obj.'Last Name' = $order1.'Last Name'
$obj.'Legal First Name' = $order1.'Legal First Name'
$obj."Anglicized Western First Name" = $order1."Anglicized Western First Name"
$obj.'Last/First Legal' = $order1.'Last/First Legal'
$obj.'Job Title' = $order1.'Job Title'
$obj.'Job Location' = $order1.'Job Location'
Write-Host "Match Found Orders " "$matchCounter"
$obj | Export-Csv -Path C:\Users\renu\Desktop\renu\Report_Orders.csv -Append -NoTypeInformation
}
}
}
$end = [system.datetime]::Now
$resultTime = $end - $start
Write-Host "Execution took : $($resultTime.TotalSeconds) seconds."

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RenuMurali-7202 commented

Make two passes over the $OrdersA hash:

 # find matched & unmatched users in this list (i.e. difference)
 $OrdersA = @{ }
 Import-Csv -Path C:\Junk\Masterlist.csv |
     ForEach-Object {
         $OrdersA[$_.'e-mailaddress'] = $_ |
             Select-Object "e-mailaddress", "Last Name", "Legal First Name", "Anglicized Western First Name", "Last/First Legal", "Job Title", "Job Location", @{n = 'Matched'; e = { $false } }
     }
 $start = [system.datetime]::Now
 Import-Csv -Path C:\Junk\ALLUserMain30082021.csv | # using this list as the reference
 ForEach-Object {
     if ($OrdersA.ContainsKey($_.EmailAddress)) {
         $OrdersA[$_.EmailAddress].Matched = $true
     }
 }
 $OrdersA.GetEnumerator() |
     ForEach-Object {
         if ($_.Value.Matched){
             $_.Value
         }
     } | Export-Csv -Path C:\Junk\Matched_Report_Orders.csv -NoTypeInformation
 $OrdersA.GetEnumerator() |
     ForEach-Object {
         if (-not $_.Value.Matched){
             $_.Value
         }
     } | Export-Csv -Path C:\Junk\Not_Matched_Report_Orders.csv -NoTypeInformation
        
 $end = [system.datetime]::Now
 $resultTime = $end - $start
 Write-Host "Execution took : $($resultTime.TotalSeconds) seconds."
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TThank you Rich.
The script works perfectly.

Regards,
Renu

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RenuMurali-7202 commented

Is it okay to place matched and unmatched in the same CSV? If not you can do the enumeration of the hash twice and select only the true/false values of each item in the hash, exporting them into separate CSVs.

 # find matched & unmatched users in this list (i.e. difference)
 $OrdersA = @{}
 Import-Csv -Path C:\Junk\Masterlist.csv |
     ForEach-Object{
         $OrdersA[$_.'e-mailaddress'] = $_ |
             Select-Object "e-mailaddress", "Last Name", "Legal First Name", "Anglicized Western First Name", "Last/First Legal", "Job Title", "Job Location", @{n='Matched';e={$false}}
     }
 $start = [system.datetime]::Now
 Import-Csv -Path C:\Junk\ALLUserMain30082021.csv |   # using this list as the reference
     ForEach-Object{
         if ($OrdersA.ContainsKey($_.EmailAddress)){
             $OrdersA[$_.EmailAddress].Matched = $true
         }
     }
 $OrdersA.GetEnumerator() |
     ForEach-Object{
         $_.Value
     } | Export-Csv -Path C:\Junk\Report_Orders.csv -NoTypeInformation
    
 $end = [system.datetime]::Now
 $resultTime = $end - $start
 Write-Host "Execution took : $($resultTime.TotalSeconds) seconds."
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Rich,
Thank you so much for the script.
This script works perfectly fine but i want 2 csv reports one for true and one for false.
This report contains both the values.
how can we have 2 seperate files?


Thanks in Advance,
Renu.

0 Votes 0 ·