Add Secondary Email Address for Existing Distribution Group
Add Secondary Email address for existing distribution group. Do not remove existing emails.
Code Used
$list = Import-Csv "C:\InputFile.csv" foreach($item in $list) { $Name ="" $ExternalAddress ="" $Name =$item.Name $ExternalAddress =$item.ExternalAddress Set-Distributiongroup $Name -EmailAddresses @{add=$ExternalAddress} -confirm:$false Get-Distributiongroup $Name | fl EmailAddresses Write-Host "Secondary EmailAddress $ExternalAddress added" } |
Error
Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "System.Collections.Hashtable" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "MultiValuedProperty collections cannot contain null values.Parameter name: item" + CategoryInfo : InvalidData: (:) [Set-DistributionGroup], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-DistributionGroup
Summary
There are many ways of doing it. The version of PowerShell used in Version 2.0. That's not an issue here. Looking into the error message the code is passing NULL values. So it's bettter to check the Input file.
Fix
The input file used is a manually created CSV file using Notepad. System.Collections.Hashtable. So the header is Name and Value. Simply fix the input file and execute the below code:
Import-Csv "C:\InputFile.csv" | %{Set-DistributionGroup $_.name -EmailAddresses @{add= $_.externaladdress -Confirm:$false -Force }} |
Enjoy PowerShell :)