Handling Duplicates When Importing Contacts
I need to import about 1500 Contacts into our directory from a .csv file. Some of the Contacts will already be in our directory so I'd like a way to, say, create a file of those that already exist so I can act further on them. I'm using this line to import contacts -Import-Csv d:\test.csv | ForEach { New-MailContact -ExternalEmailAddress $_.ExternalEmailAddress -Name $_.Name -OrganizationalUnit "Victims" }Is there some way to keep track of those that do not get created?Orange County District Attorney
March 4th, 2010 10:00pm

$not_created = @()Import-Csv d:\test.csv | ForEach {if (get-mailcontact $_.externalemailaddress){$not_created += $_}else{ New-MailContact -ExternalEmailAddress $_.ExternalEmailAddress -Name $_.Name -OrganizationalUnit"Victims" }}$not_created | export-csv "c:\somedir\not_created.csv" -notypeI will take your problems into my head and into my hands, but I will not take them into my stomach.
Free Windows Admin Tool Kit Click here and download it now
March 5th, 2010 3:39am

This is perfect. Thanks for the code!Orange County District Attorney
March 5th, 2010 6:34pm

Glad to help :).I will take your problems into my head and into my hands, but I will not take them into my stomach.
Free Windows Admin Tool Kit Click here and download it now
March 5th, 2010 6:36pm

$not_created = @() Import-Csv d:\test.csv | ForEach { if (get-mailcontact $_.externalemailaddress){$not_created += $_} else{ New-MailContact -ExternalEmailAddress $_.ExternalEmailAddress -Name $_.Name -OrganizationalUnit"Victims" } } $not_created | export-csv "c:\somedir\not_created.csv" -notype I will take your problems into my head and into my hands, but I will not take them into my stomach. Hi mjolinor, I have a similar requirement as Sandy Wood however I was wondering if you could help me understand the above?? I am a newby to powershell scripting and I am trying to get my head around what is going on around the not_created = @() part of the script??
April 21st, 2011 7:08am

$not_created = @() initializes a new, empty array. import-csv will create an array of ps custom objects. You can add ps custom objects to an array using the += operator, but you cannot add a pscustom object to another ps custom object. If you don't make $not_created an array first, then the first time you add to it, it will be created with the object type of the object that was added. Then when you try to add the next one, it will fail because that's an invalid operation - you're trying to add a ps custom object to another ps custom object.[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Free Windows Admin Tool Kit Click here and download it now
May 5th, 2011 6:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics