Automating user creation.

Marin Marinov 161 Reputation points
2024-01-02T16:44:41.1133333+00:00

Happy New Year!

I`m currently working on a script for MS 365 users creation automation using an excel file. Here is summary of the tasks that the script should performs.

  • import an excel file for later use
  • create accounts using the information listed in the excel file
  • assign a license to all of the users
  • add each user to a Microsoft unified/security group based on the the account property "department" (each department is called exactly like the group. e.g. department: "HR" / group "HR")
  • assign a manager to each each user based on account property "department".

Below you can see what I archived on my own.

#below code creates the accounts and assigns them a license 

NewUsers = Import-Excel -Path "B:\PowerShell\users.xlsx"

$PasswordProfile = @{
    Password = "Helo123!"
    ForceChangePasswordNextSignIn = $true
    ForceChangePasswordNextSignInWithMfa = $true
	}

foreach ($user in $NewUsers) {
	New-MgUser -UsageLocation $user.UsageLocation `
	-GivenName $user.GivenName `
	-Surname $user.SurName `
	-DisplayName $user.DisplayName `
	-MailNickName $user.MailNickName `
	-UserPrincipalName $user.UserPrincipalName `
	-PasswordProfile $PasswordProfile `
	-Department $user.Department `
	-AccountEnabled 
	}

Write-Host " "
Write-Host "Accounts Creation Completed. License assignment in progress...." -ForegroundColor Green
Write-Host " "

$License = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'ENTERPRISEPREMIUM'

foreach ($user in $NewUsers) {
		Set-MgUserLicense -UserId $user.UserPrincipalName `
		-AddLicenses @{SkuId = $License.SkuId} `
		-RemoveLicenses @() 
	}

Write-Host " "
Write-Host "All accounts have been assigned with liceses." -ForegroundColor Green


I`m struggling to figure out how to add each user to a group based on the department property. This is my attempt to add them to a groups. Unfortunately it does not work. According to the error message it is not getting correctly the group ID.

foreach ($user in $NewUsers) {
    $Groupid = (Get-MgGroup | Where-Object {$_.DisplayName -eq $user.Department}).id

    $upn = $user.Id

    $params = @{
		"@odata.id" = "https://graph.microsoft.com/v1.0/users/$upn"
	}

	New-MgGroupMemberByRef -GroupId $GroupId -BodyParameter $params
}

Regarding the last part (assigning manager based on the department) I could not figure out noting.

Can you help me out?

Thank you in advance!

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marin Marinov 161 Reputation points
    2024-01-03T15:40:11.53+00:00

    It`s done!

    #connecting to Graph using the corresponding permissions
    Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All
    
    #importing the excel file containing the user and group information
    $NewUsers = Import-Excel -Path "B:\PowerShell\users.xlsx"
    
    #creating password; it will be the same for all users
    $PasswordProfile = @{
        Password = "Helo123!"
        ForceChangePasswordNextSignIn = $true
        ForceChangePasswordNextSignInWithMfa = $true
    	}
    
    #creating all users;
    foreach ($user in $NewUsers) {
    	New-MgUser -UsageLocation $user.UsageLocation `
    	-GivenName $user.GivenName `
    	-Surname $user.SurName `
    	-DisplayName $user.DisplayName `
    	-MailNickName $user.MailNickName `
    	-UserPrincipalName $user.UserPrincipalName `
    	-PasswordProfile $PasswordProfile `
    	-Department $user.Department `
    	-AccountEnabled 
    	}
    
    Write-Host " "
    Write-Host "Accounts Creation Completed. License assignment in progress...." -ForegroundColor Green
    Write-Host " "
    
    #getting the license
    $License = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'ENTERPRISEPREMIUM'
    
    #assigning the license 
    foreach ($user in $NewUsers) {
    		Set-MgUserLicense -UserId $user.UserPrincipalName `
    		-AddLicenses @{SkuId = $License.SkuId} `
    		-RemoveLicenses @() 
    	}
    
    Write-Host " "
    Write-Host "All accounts have been assigned with liceses." -ForegroundColor Green
    
    #importing the required information(the tab called "groups")
    $NewGroups = Import-Excel -Path "B:\PowerShell\users.xlsx" -WorksheetName "Groups"
    
    Write-host "Group creation based on the excel file  in progress..." -ForegroundColor Green
    
    #creating groups based on the provided information + assigning group owner
    foreach ($group in $NewGroups){
    
        New-MgGroup -DisplayName $group.DisplayName `
        -MailEnabled:$False  `
        -MailNickName $group.MailNickName `
        -SecurityEnabled `
        -GroupTypes Unified
    
    
        $Owner = (Get-MgUser | Where-Object {$_.DisplayName -eq $group.Owner}).ID
        $Groupid = (Get-MgGroup | Where-Object {$_.DisplayName -eq $group.DisplayName }).Id
        $params = @{ "@odata.id" = "https://graph.microsoft.com/v1.0/users/$Owner"}
    
        New-MgGroupOwnerByRef -GroupId $GroupId -BodyParameter $params 
    }
    
    #adding users to the previously created groups
    
    foreach ($user in $NewUsers) {
    
        $DepartmentName = $user.Department
        $Groupid = (Get-MgGroup | Where-Object {$_.DisplayName -eq $DepartmentName}).Id
    
        $upn = $user.UserPrincipalName
    
        $params = @{
    		"@odata.id" = "https://graph.microsoft.com/v1.0/users/$upn"
    	}
    
    	New-MgGroupMemberByRef -GroupId $GroupId -BodyParameter $params 
        Write-Host "A user has been added to '$($DepartmentName)'" -ForegroundColor Green
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    0 comments No comments

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.