none
Office 365 Scripting - Add Single User to multiple distribution groups RRS feed

  • Question

  • I have used a script successfully to add a user to multiple distribution groups with the below script. 

    #asks for office 365 credentials and stores them in $UserCredentials
    $UserCredential = Get-Credential
    
    #Takes credentials and uses them to connect to the online service.
    Connect-MsolService -Credential $UserCredential

    $Variable = "<Distribution Group name>","<Distribution Group name>","<Distribution Group name>" ForEach ($item in $Variable) {Add-DistributionGroupMember -Identity $item –Member <Identity>}


    I have tried to add some logic into it slightly easier to use for my purpose. The code for that is below.

    #asks for office 365 credentials and stores them in $UserCredentials
    $UserCredential = Get-Credential
    
    #Takes credentials and uses them to connect to the online service.
    Connect-MsolService -Credential $UserCredential
    
    #Prompt for email address of member to be added to distro groups
    $MemberToBeAdded = Read-Host -Prompt 'Input the email address of the user you want to add to groups'
    
    #Prompts for s or d, s for standard group d for development group"
    $ChoiceInput = Read-Host -Prompt "Do you want to add user to developement groups or standard groups?, press s for standard d for development"
    $Choice = $ChoiceInput.ToLower()
    
    
    #standard list of distibution groups for birmingham employees
    $StandardGroups = "DistributionGroup","DistributionGroup","DistributionGroup","DistributionGroup"
    
    #standard list of birmingham distribution groups 
    $DevelopmentGroups = "DistributionGroup","DistributionGroup","DistributionGroup","DistributionGroup", "DistributionGroup", "DistributionGroup", "DistributionGroup"
    
    #variable to set based on selection and assignment in if else statemtent
    $GroupsSelection = ""
    
    #if statement assigns $Standard to $GroupSelection else assigns $GroupSelection to $DevelopmentGroups
    if ($Choice = "s") {$StandardGroups = $GroupsSelection}
    else {$DevelopmentGroups = $GroupsSelection}
    ForEach ($item in $GroupsSelection)
    
    #takes every group in $item and assigns the -Member to that group
    {Add-DistributionGroupMember -Identity $item –Member $MemberToBeAdded

    The error that <g class="gr_ gr_69 gr-alert gr_tiny gr_spell gr_run_anim gr_inline_cards ContextualSpelling multiReplace" data-gr-id="69" id="69">i</g> receive after entering email and entering my selection is below. 

    Add-DistributionGroupMember : The term 'Add-DistributionGroupMember' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, 
    or if a path was included, verify that the path is correct and try again.
    At C:\Users\jmoorehead\Desktop\Desktop\Scripts\Powershell Scripts\Add_Single_User_To_Multiple_DistributionGroups.ps1:30 char:2
    + {Add-DistributionGroupMember -Identity $item –Member $MemberToBeAdded ...
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Add-DistributionGroupMember:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException


    • Edited by Joshua Moorehead Wednesday, March 1, 2017 2:44 PM
    • Moved by Bill_Stewart Friday, April 28, 2017 6:08 PM User apparently solved own problem
    Monday, February 27, 2017 6:59 PM

All replies

  • The error message tells you exactly what is wrong.

     Theterm 'Add-DistributionGroupMember' is notrecognized asthe name of a cmdlet, function

    You do not have the Exchange support installed.  Connect to Office 365 first then the command will be available.


    \_(ツ)_/

    Monday, February 27, 2017 8:34 PM
  • The top script works and the bottom one doesnt they both only use the below to connect the they both use Add-DistributionGroupMember. Grammarly was causing that weird html. My apologies. 
    #asks for office 365 credentials and stores them in $UserCredentials
    $UserCredential = Get-Credential
    
    #Takes credentials and uses them to connect to the online service.
    Connect-MsolService -Credential $UserCredential



    Monday, February 27, 2017 9:35 PM
  • You have to fix your posts.  You are posting broken HTML tags and your posts cannot be read.

    This is what we see:

    g class="gr_ gr_3 gr-alert gr_spell gr_run_anim gr_inline_cards ContextualSpelling ins-del multiReplace" data-gr-id="3" id="3">doesnt</g>

    Which is just garbage.


    \_(ツ)_/

    Monday, February 27, 2017 9:40 PM
  • The command is part of O365/ExchangeOnline and not part of MSOL.  Connect to O365 and it will be available.


    \_(ツ)_/

    Tuesday, February 28, 2017 6:47 PM
  • #asks for office 365 credentials and stores them in $UserCredentials
    $UserCredential = Get-Credential
    #sets remote execution policy
    Set-ExecutionPolicy RemoteSigned
    
    #creates exchanges session using user credentials
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    #imports the session
    Import-PSSession $Session
    #Prompt for email address of member to be added to distro groups
    $MemberToBeAdded = Read-Host -Prompt 'Input the email address of the user you want to add to groups'
    
    #Prompts for s or d, s for standard group d for development group"
    $ChoiceInput = Read-Host -Prompt "Do you want to add user to developement groups or standard groups?, press s for standard d for development"
    $Choice = $ChoiceInput.ToLower()
    
    #if statement assigns $Standard to $GroupSelection else assigns $GroupSelection to $DevelopmentGroups
    if ($Choice = "s") {
    
        $StandardGroups = "DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups"
        ForEach ($item in $StandardGroups)
    
        #takes every group in $item and assigns the -Member to that group
        {Add-DistributionGroupMember -Identity $item –Member $MemberToBeAdded}
    
    }
    else {
    
        $DevelopmentGroups = "DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups","DistributionGroups"
        ForEach ($item in $DevelopmentGroups)
    
        #takes every group in $item and assigns the -Member to that group
        {Add-DistributionGroupMember -Identity $item –Member $MemberToBeAdded}
    
    }
    
    
    Remove-PSSession $Session
    Above worked for me after much searching. 
    Thursday, March 2, 2017 10:40 PM