Home > Net >  Add the email address for multiple security groups from a csv file using powershell
Add the email address for multiple security groups from a csv file using powershell

Time:02-12

I have a csv file with a list of groups with their email addresses. I need add these email addresses to the mail attribut security group in AD (no exchange). In the csv the first column is labeled group (###.testgroup) second column is labeled mail (###[email protected]).

When I run this script

Import-CSV -path 'C:\Temp\test.csv' | ForEach-Object {Set-ADGroup -Identity $_.group -Replace @{mail="$_.mail"}}

It returns the following instead of the actual email address the mail field.

@{group=###.testgroup; mail=domain.com}.mail

CodePudding user response:

Your problem is because of the double quotes around "$.mail" which cast your object "$" to a string and then add litterally ".mail" at the end of the string. To bypass this behaviour, (for example when you want to use write-host) you can use this :

"$($_.mail)"

Or in your case, you can simply remove the quotes which gives:

Import-CSV -path 'C:\Temp\test.csv' | ForEach-Object {Set-ADGroup -Identity $_.group -Replace @{mail=$_.mail}}
  • Related