locked
Add user to multiple groups in one command RRS feed

  • Question

  • I'm just starting to learn powershell and want to create a script to make it easy to our helpdesk to create new users in Office 365.

    I want them to be able to add a newly created user to different Office 365 groups.

    So far I've got this working.

    This gets them a list of O365 groups available:
    Get-UnifiedGroup

    This will allow them to add all groups they want the user to be added to in a variable:
    $userInput = read-host "Enter a list of comma separated hostnames"
    $data = $userInput.split(",").trim(" ")


    Now I want to add each row in $data to be added in the -Identity field in this command:
    Add-UnifiedGroupLinks -Identity $data -LinkType Member -Links $UserPrincipalName

    How do I achieve this?
    I have tried:

    ForEach ($line in $data) {
    Add-UnifiedGroupLinks -Identity $data -LinkType Member -Links $UserPrincipalName
    }

    But it tries to add both lines in the same command.

    Monday, October 22, 2018 10:07 AM

Answers

  • Specify $line instead of $data in the loop to treat it per line instead of the full dataset.

    ForEach ($line in $data) {
        Add-UnifiedGroupLinks -Identity $line -LinkType Member -Links $UserPrincipalName
    }
    Monday, October 22, 2018 10:47 AM

All replies

  • You have to do this in a loop.  You can only add one name at a time.


    \_(ツ)_/

    Monday, October 22, 2018 10:41 AM
  • Specify $line instead of $data in the loop to treat it per line instead of the full dataset.

    ForEach ($line in $data) {
        Add-UnifiedGroupLinks -Identity $line -LinkType Member -Links $UserPrincipalName
    }
    Monday, October 22, 2018 10:47 AM
  • Thank you, sir!
    Monday, October 22, 2018 11:27 AM