【Office365】PowerShellでアカウントを一括削除

Office365
スポンサーリンク


年度末の退職等でユーザーを大量に削除する場合に役立つ、Powershellを使ってまとめてユーザーを削除する方法です。

スポンサーリンク

Azure ADに接続

Graph用Azure Active Directory PowerShellモジュールを使用します。
まずはPowerShellを起動してAzure ADに接続します。

接続方法についてはこちらをご覧ください。

【Office365】PowershellでGraph用Azure Active Directory(Azure AD)に接続する方法
Graph用Azure Active Directory PowerShellへの接続方法についてご紹介します。 MSOnline Moduleを使用するためのAzure AD接続方法についてはこちらをご覧ください。 Pow...

CSVファイルを準備

パスワードを変更するユーザの一覧をCSV形式で準備します。

UserPrincipalName
user1@example.com
user2@example.com
user3@example.com

CSVファイルはUTF-8形式で保存してください。
※Excelで作成して、保存する時に「CSV UTF-8(コンマ区切り)(*.csv)」を選択

PowerShellコマンドの実行

下記の構文を環境に合わせて書き換えてください。

Import-Csv -Path “CSVファイルパス” | ForEach-Object {Remove-AzureADUser -ObjectID $_.UserPrincipalName;}

Import-Csv -Path "CSVファイルパス" | ForEach-Object {Remove-AzureADUser -ObjectID $_.UserPrincipalName;}

例えばCSVファイルをCドライブのTempフォルダに「RemoveUser.csv」というファイル名で保存した場合

Import-Csv -Path “C:\temp\RemoveUser.csv” | ForEach-Object {Remove-AzureADUser -ObjectID $_.UserPrincipalName;}

Import-Csv -Path "C:\temp\RemoveUser.csv" | ForEach-Object {Remove-AzureADUser -ObjectID $_.UserPrincipalName;}

UserPrincipalNameが合致するユーザーを削除します。