Commands to import CSV file to contacts then export the contact list for comparison

Modified on Tue, 30 Jun at 2:58 PM



#Connect To Exchange Online

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session


#Function to pick the CSV File

Function Get-FileName($initialDirectory)

{

[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |

Out-Null


$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

$OpenFileDialog.initialDirectory = $initialDirectory

$OpenFileDialog.filter = “All files (*.*)| *.*”

$OpenFileDialog.ShowDialog() | Out-Null

$OpenFileDialog.filename

} #end function Get-FileName

#Command To Launch Function and store it in the variable

$PathToCSV = Get-FileName -initialDirectory "C:\"


#Commands to import CSV file to contacts then export the contact list for comparison

Import-Csv $PathToCSV | %{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}

Get-MailContact | Select DisplayName,ExternalEmailAddress,FirstName,LastName | Out-GridView

Get-MailContact | Select DisplayName,ExternalEmailAddress | Export-Csv "C:\Contacts\ExportedContacts.csv"


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article