2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Microsoft 365】Microsoft 365 ユーザーアカウントを PowerShell で作成する

Last updated at Posted at 2021-07-15

準備

Microsoft OnlineService モジュールをインストールする

以下のコマンドでモジュールをインストール、または更新してください。

# Microsoft OnlineService のインストールと更新
if((Get-Module MSOnline -ListAvailable | measure).Count -eq 0) {
    Install-Module MSOnline -Force
} else {
    Update-Module MSOnline
}
Get-Command -Module MSOnline | select Name, Version

ユーザーアカウントの新規作成

Microsoft Azure Active Directory に接続する

$Credential = Get-Credential
Connect-MsolService -Credential $Credential

有効ライセンスの取得

ユーザーに付与するライセンスの ID を取得しておきます。{テナント名}:{ライセンスの種類}

$License = (Get-MsolAccountSku).AccountSkuId

ロールの確認

ユーザを追加する権限グループの情報を取得しておきます。

Get-MsolRole | Sort Name | Select Name, Description

ユーザーを作成する

CSV ファイルのデータを元にユーザーを作成します。以下のようなデータのファイルを用意します。

TestUsers.csv
UserPrincipalName Password LastName FirstName DisplayName License Title Department Office PhoneNumber MobilePhone Fax PostalCode State City StreetAddress Country UsageLocation RoleName
testUser001@{テナント}.onmicrosoft.com ●●●●● テスト 001 テスト001 {テナント}:{ライセンス} テストユーザー 〇〇部 本社 03-1234-5678 000-0000-0000 03-9876-5432 101-0022 東京都 千代田区 3 日本 JP SharePoint Administrator
testUser002@{テナント}.onmicrosoft.com ●●●●● テスト 002 テスト002 {テナント}:{ライセンス} テストユーザー 〇〇部 本社 03-1234-5678 000-0000-0000 03-9876-5432 101-0022 東京都 千代田区 3 日本 JP SharePoint Administrator
testUser003@{テナント}.onmicrosoft.com ●●●●● テスト 003 テスト003 {テナント}:{ライセンス} テストユーザー 〇〇部 本社 03-1234-5678 000-0000-0000 03-9876-5432 101-0022 東京都 千代田区 3 日本 JP SharePoint Administrator

以下のコマンドを実行します。

ipcsv '.\TestUsers.csv' -Encoding Default | % {
    $UserPrincipalName = $_.UserPrincipalName	# ユーザー名 ※必須
    $Password = $_.Password						# パスワード
    $LastName = $_.LastName						# 姓
    $FirstName = $_.FirstName					# 名
    $DisplayName = $_.DisplayName				# 表示名 ※必須
    $LicenseAssignment = $_.License				# ライセンス付与
    $Title = $_.Title							# 役職
    $Department = $_.Department					# 部署
    $Office = $_.Office                         # 事務所
    $PhoneNumber = $_.PhoneNumber				# 電話番号
    $MobilePhone = $_.MobilePhone				# 携帯電話番号
    $FAX = $_.FAX								# FAX番号
    $PostalCode = $_.PostalCode					# 郵便番号
    $State = $_.State							# 都道府県
    $City = $_.City								# 市区町村
    $StreetAddress = $_.StreetAddress			# 番地
    $Country = $_.Country						# 国
    $UsageLocation = $_.UsageLocation			# 利用地域 日本であれば [JP] ※必須
    $StrongPasswordRequired = $false			# パスワードの複雑性の有効化/無効化
    $ForceChangePassword = $false				# 初回ログイン時にパスワード変更を要求するかどうか
    $PasswordNeverExpires = $true				# パスワードの有効期限を無効化するかどうか

    New-MsolUser `
        -UserPrincipalName $UserPrincipalName `
        -Password $Password `
        -LastName $LastName `
        -FirstName $FirstName `
        -DisplayName $DisplayName `
        -LicenseAssignment $LicenseAssignment `
        -Title $Title `
        -Department $Department `
        -Office $Office `
        -PhoneNumber $PhoneNumber `
        -MobilePhone $MobilePhone `
        -FAX $FAX `
        -PostalCode $PostalCode `
        -State $State `
        -City $City `
        -StreetAddress $StreetAddress `
        -Country $Country `
        -UsageLocation $UsageLocation `
        -StrongPasswordRequired $StrongPasswordRequired `
        -ForceChangePassword $ForceChangePassword `
        -PasswordNeverExpires $PasswordNeverExpires `

    $RoleName = $_.RoleName
    if (![string]::IsNullOrEmpty($RoleName)) {
        Add-MsolRoleMember -RoleMemberEmailAddress $UserPrincipalName -RoleName $RoleName
    }
}

参考サイト

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?