본문 바로가기
ICT

[PowerShell] Creating M365 Accounts Bulkily via PowerShell

by NeoSailer 2026. 2. 24.

[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

반응형

댓글