Home > OS >  multiple members in New-DistributionGroup not working
multiple members in New-DistributionGroup not working

Time:09-20

I am trying to create a new distribution group from one CSV using the following code.

 Import-Csv -Path C:\Users\Lenovo\Desktop\CIS\script\csv.csv | ForEach { New-DistributionGroup -Name $_.name -Members $_.members }

and my CSV has the following data.

name            members
ITDepartment    [email protected],[email protected]

but when I run the above command it always gives me the below error.

Couldn't find object "[email protected],[email protected]". Please make sure that it was spelled correctly or specify a different object.
  CategoryInfo          : NotSpecified: (:) [New-DistributionGroup], ManagementObjectNotFoundException
  FullyQualifiedErrorId : [Server=AM6PR08MB4279,RequestId=4dc85b89-0791-4a2b-9409-beaf728789aa,TimeStamp=19-09-2022 14:31:12] [FailureCategory=Cmdlet-ManagementObjectNotFoundEx 
ception] 10D55A27,Microsoft.Exchange.Management.RecipientTasks.NewDistributionGroup
  PSComputerName        : outlook.office365.com

I don't where am I doing wrong here.

CodePudding user response:

What you are facing is a content issue in the CSV file. The two email IDs are although separated with comma but powershell is not able to parse it properly because it is considering both the emails IDs together as one single string.

Kindly work on handling that and try with a sample file with clean and structured content. It will push through.

I do not see any issue in the script line however if you are considering it to be a robust script for future then try having proper error handling, logs and other necessary things so that it would be easier for you to debug specific objects in future.

CodePudding user response:

Ranadip Dutta's helpful answer provides the context as to why the code is failing, the -Members parameter from the New-DistributionGroup cmdlet accepts multiple values (array of values) when adding multiple members, however, what you have in your CSV is a single string having two members concatenated with a comma. You can however convert this comma delimited string into an array using the .Split string method:

Import-Csv -Path C:\Users\Lenovo\Desktop\CIS\script\csv.csv | ForEach-Object {
    New-DistributionGroup -Name $_.name -Members $_.members.Split(',')
}
  • Related