I have 2 CSV files with user names.
I want to export just the names of users who don't exist in both files.
The code I have now:
$file1 = import-csv -Path "C:\ps\output\adusers.csv"
$file2 = import-csv -Path "C:\ps\output\users.csv"
Compare-Object $file1 $file2 -property name | Export-Csv -NoTypeInformation -Path "C:\ps\result\result.csv"
Try something like this to use
Compare-Object
for only the differences from file1:If you want all differences (from both files) remove this part:
Use the SideIndicator to get the differences.
Note: You can use the sort -unique also in your case.
Hope it helps.
Use
Select-Object name
to extract only the name field from Compare-Object's output:Notes:
sort -unique
deduplicates and sorts the list.If you do not mind using linq/NET, this will list users present in BOTH lists:
Linq does not have method opposite/reverse to intersect but you can use SymmetricExceptWith from generic collections.
Code below lists users who are present either in one list or another but not in both at once:
Note: this code may not work on earlier powershell versions.