[Requirements]
A customer purchased bundle of M365 licenses and they want to create 30+ users.
To avoid human error and reduntant tasks, PowerShell script can be manipulated.
[OS]
N/A
[Development Language]
PowerShell
[IDE]
Visual Studio Code/PowerShell extension
** PowerShell 7 does not support PowerShell ISE
[Setting]
Installing modules
# Installation
Install-Module Microsoft.Graph -Scope CurrentUser
Install-Module Microsoft.Graph.Beta
Install-Module Microsoft.Graph -Scope CurrentUser -Force
# Verification
Get-InstalledModule Microsoft.Graph
Get-Module -ListAvailable Microsoft.Graph
# Import
Import-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Users
Creating csv file having UserPrincipalName, FirstName, LastName, DisplayName columns.
[Code]
# Connection
Connect-MgGraph -Scopes "User.ReadWrite.All","Organization.Read.All"
# CSV file
$users = Import-Csv "\\192.168.1.250\it\M365\NewUsers.csv"
# 일반 라이선스 SKU
$skuId = "f245ecc8-75af-4f8e-b61f-27d8114de5f3"
foreach ($user in $users) {
if ([string]::IsNullOrWhiteSpace($user.UserPrincipalName)) {
Write-Host "UPN missing for $($user.DisplayName)" -ForegroundColor Red
continue
}
$params = @{
DisplayName = $user.DisplayName
GivenName = $user.FirstName
Surname = $user.LastName
UserPrincipalName = $user.UserPrincipalName
MailNickname = $user.UserPrincipalName.Split("@")[0]
AccountEnabled = $true
UsageLocation = "KR"
PasswordProfile = @{
Password = $user.Password
ForceChangePasswordNextSignIn = $true
}
}
New-MgUser @params
# 약간 대기 (안정성)
Start-Sleep -Seconds 2
# 라이선스 할당
Set-MgUserLicense -UserId $user.UserPrincipalName -AddLicenses @{SkuId=$skuId} -RemoveLicenses @()
Write-Host "$($user.UserPrincipalName) created and licensed" -ForegroundColor Green
}
[Test]
After running the script checked the active users in M365 Admin. center and found test account created.
[Lesson Learned]
Don't do manual work. There is mighty PowerShell
반응형
'ICT' 카테고리의 다른 글
| [M365] Disabling Exchange Online(plan1) (0) | 2026.02.25 |
|---|---|
| [M365] Mail Flow Troubleshooting in M365 Environment (0) | 2026.02.24 |
| [PowerShell] SmartBill Port Configuration (0) | 2025.11.18 |
| [SAP B1] Customer Refund Report Query (0) | 2025.11.06 |
| [Automation] SAP Pending Approval Notification (0) | 2025.10.07 |
댓글