Adding multiple users to multiple Teams through Powershell using .csv

Viewed 59

As mentioned in the title, I am trying to add multiple users to multiple Teams through Powershell using a .csv file. The problem I am having is that my .csv includes only the Team name, and not the GroupID. I need to be able to reference the team name and look up the corresponding GroupID, and then have that ID used in the add user phase. Is this even possible?

Here is what I have worked up so far:

#Read team users from CSV file
$TeamUsers = Import-CSV "File_Path"
$i = 0;
$TotalRows = $TeamUsers.Count
#Iterate team user details one by one
Foreach($TeamUser in $TeamUsers)
{

$TeamName = $TeamUser.'TeamName'
$GroupId = (Get-Team -DisplayName 'TeamDesc').GroupId
$UserId = $TeamUser.'UserPrincipalName'
$Role = $TeamUser.'Role'

$i++;
Write-Progress -activity "Processing $TeamName - $UserId - $Role" -status "$i out of $TotalRows completed"
Try
{
#Add user to the team
Add-TeamUser -GroupId $GroupId -User $UserId -Role $Role
}
catch
{
Write-Host "Error occurred for $TeamName - $UserId" -f Yellow
Write-Host $_ -f Red
}
}

The headers of my .csv include "UserPrincipleName", "TeamDesc", and "Role". The Team name is stored in the "TeamDesc" column.

1 Answers

There are 2 improvements I could think of for your code, the first one, currently you're getting the GroupId of the Teams groups per each element in your loop, this is a bit inefficient assuming there could be multiple rows in the CSV having the same group. Instead, we could first group all objects in the CSV having the same group so we only query them once, for this we can use Group-Object.

The second improvement would be to first check if Get-Team returned any results, if it didn't we can skip trying to add a user (or multiple users!) to a Team that doesn't exist, this could save you a bunch of error being displayed on the console.

You could also benefit from Splatting, though this doesn't make the code more "efficient" it does help with making the code more readable.

# Read team users from CSV file
$TeamUsers = Import-CSV "File_Path"
$i = 0
# Group the objects by their TeamDesc property, so we only query each group once
$TeamUsers | Group-Object TeamDesc | ForEach-Object {
    # Get the GroupId of this Group
    $groupId = (Get-Team -DisplayName $_.Name).GroupId
    # If the Group couldn't be found, just skip below logic
    if(-not $groupId) { return }
    # If the Group could be found, create a hashtable for future splatting
    $params = @{ GroupId = $groupId }
    
    # now we can enumerate each object in each group of objects
    foreach($user in $_.Group) {
        try {
            # create a hashtable for splatting progress
            $progress = @{
                PercentComplete = $i++ / $TeamUsers.Count * 100
                Activity        = 'Adding Users to MS Teams!!'
                Status          = 'Working on Team: "{0}" and User: "{1}"' -f $_.Name, $user.UserPrincipalName
            }
            Write-Progress @progress

            # add this user with this role to the hashtable
            $params['User'] = $user.UserPrincipalName
            $params['Role'] = $user.Role
            Add-TeamUser @params
        }
        catch {
            ('Error occurred for {0} - {1}' -f $user.TeamName, $user.UserPrincipalName),
            $_.ToString() | Write-Warning
        }
    }
}
Related