Powershell to change email of AD Groups in a particular OU

Lucas Cruz 61 Reputation points
2022-07-16T12:52:49.96+00:00

I'm having trouble executing the command to add email to groups in the active directory, I run the command below, but it returns the error below

script:

Get-ADGroup -Filter * -SearchBase 'OU=Myou' | ForEach-Object { Set-ADGroup -Identity -mail ($_.sAMAccountName + '@teste .com') }

error:

Set-ADGroup : A positional parameter cannot be found that accepts argument 'teste@teste .com'.
At line:1 char:128

  • ... ch-Object { Set-ADGroup -Identity -mail ($_.sAMAccountName + '@teste . ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADGroup], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADGroup

Set-ADGroup : A positional parameter cannot be found that accepts argument 'teste-grupomail@teste .com'.
At line:1 char:128

  • ... ch-Object { Set-ADGroup -Identity -mail ($_.sAMAccountName + '@teste . ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADGroup], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADGroup
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
0 comments No comments
{count} votes

Accepted answer
  1. Dillon Silzer 57,831 Reputation points Volunteer Moderator
    2022-07-16T19:14:11.73+00:00

    Hello @Lucas Cruz

    Please use the following script below to achieve this:

    $groups = Get-ADGroup -filter *  -SearchBase "OU=path,DC=domain,DC=com" | select -exp samaccountname  
    ForEach ($group in $groups) {  
    	Set-ADGroup -Identity $group -Replace @{mail=$group + '@example.com'}  
    }  
    

    Borrowed parts of the code from https://community.spiceworks.com/topic/2042327-pull-users-from-a-group-that-are-in-a-specific-ou and https://serverfault.com/questions/567279/how-can-a-set-a-security-groups-email-address-using-powershell and customized it to your specifications.

    -------------------------

    If this helps please don't forget to mark as correct answer.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.