Microsoft Entra PowerShell を使用してユーザーを管理する

ユーザーとは、Microsoft Entraの職場または学校のユーザー アカウント、またはMicrosoft Entra IDの個人Microsoft アカウントを表します。 Microsoft Entra PowerShell のユーザー リソースはユーザーの表現であり、ユーザーに関連するリレーションシップとリソースが含まれます。

ユーザー リソースを使用すると、追加の呼び出しを実行したり、特定の認証情報を検索したり、他のMicrosoft Entra PowerShell オブジェクトに対してクエリを直接発行したりすることなく、ユーザー リソースに簡単にアクセスして操作できます。

前提条件

Microsoft Entra PowerShell を使用してユーザーを管理するには、次のものが必要です。

ユーザーの情報にアクセスし、ユーザーの代わりに、または独自の ID を持つアプリとしてデータを管理できます。

ユーザーをオンボーディングする

ユーザーをオンボードするには、Microsoft Entra IDで新しいユーザー アカウントを作成します。 このプロセスでは、表示名、電子メール アドレス、パスワードなど、ユーザーのプロファイルを設定します。

ユーザーの作成

この例では、新しいユーザーを作成します。

Connect-Entra -Scopes 'User.ReadWrite.All'
$passwordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$passwordProfile.Password = '<Strong-Password>'
$userParams = @{
    DisplayName = 'New User'
    PasswordProfile = $passwordProfile
    UserPrincipalName = 'NewUser@contoso.com'
    AccountEnabled = $true
    MailNickName = 'NewUser'
}
New-EntraUser @userParams

出力には、新しく作成されたユーザーの詳細が表示されます。

DisplayName    Id                                     Mail    UserPrincipalName
-----------    --                                     ----    -----------------
New User       aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb           NewUser@contoso.com

ユーザーの一括作成

複数のユーザーを一括で作成するには、CSV ファイルを使用できます。 CSV ファイルには、 DisplayNameUserPrincipalNamePasswordProfileなど、必要なユーザー属性が含まれている必要があります。

# Connect to Microsoft Entra PowerShell
Connect-Entra -Scopes 'User.ReadWrite.All'

# Create a new Password Profile for the new users. We'll be using the same password for all new users in this example
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = '<Your-Password>'

# Import the csv file. You will need to specify the path and file name of the CSV file in this cmdlet
$NewUsers = import-csv -Path '<path-to-your-csv-file>'

# Loop through all new users in the file to create them in Microsoft Entra ID
ForEach ($user in $NewUsers) {
    # Create a new user in Microsoft Entra ID
    New-EntraUser -UserPrincipalName $user.'EmailAddress' -DisplayName $user.'DisplayName' -GivenName $user.'FirstName' -Surname $user.'LastName' -Department $user.'Department' -MailNickname $user.'MailNickname' -AccountEnabled $true -PasswordProfile $passwordProfile
    }
ファーストネーム 苗字 DisplayName メールアドレス 部門 メールのニックネーム
Adele Vance Adele Vance adelev@contoso.com Marketing adelev

出力には、新しく作成されたユーザーの詳細が表示されます。

DisplayName Id                                   Mail UserPrincipalName
----------- --                                   ---- -----------------
Adele Vance aaaaaaaa-1111-2222-3333-bbbbbbbbbbbb      adelev@contoso.com

ユーザーのパスワードを更新する

  1. 管理者がユーザーのパスワードを更新するには、次のコマンドを使用します。

    Connect-Entra -Scopes 'Directory.AccessAsUser.All'
    $newPassword = '<strong-password>'
    $securePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force
    Set-EntraUserPasswordProfile -UserId 'SawyerM@contoso.com' -Password $securePassword
    
  2. サインインしているユーザー (セルフサービス) のパスワードを更新するには、次のコマンドを使用します。

    Connect-Entra -Scopes 'Directory.AccessAsUser.All'
    $currentPassword = ConvertTo-SecureString '<strong-password>' -AsPlainText -Force
    $newPassword = ConvertTo-SecureString '<strong-password>' -AsPlainText -Force
    Set-EntraSignedInUserPassword -CurrentPassword $currentPassword -NewPassword $newPassword
    

    このコマンドを使用すると、ユーザーは管理者特権なしで自分のパスワードを変更できます。

ユーザーの写真をアップロードまたは取得する

  1. ユーザーの写真をアップロードします。

    Connect-Entra -Scopes 'User.ReadWrite.All'
    Set-EntraUserThumbnailPhoto -UserId 'SawyerM@contoso.com' -FilePath 'D:\UserThumbnailPhoto.jpg'
    

    次の使用例は、UserId パラメーターで指定されたユーザーのサムネイル写真を、FilePath パラメーターで指定された画像に設定します。

  2. ユーザーの写真を取得します。

    Connect-Entra -Scopes 'ProfilePhoto.Read.All'
    Get-EntraUserThumbnailPhoto -UserId 'SawyerM@contoso.com'
    

    この例では、 UserId パラメーターの値を使用して指定されたユーザーのサムネイル写真を取得する方法を示します。

組織内のユーザーに管理者ロールを付与する

組織内のユーザーに管理者ロールを付与すると、ユーザーは特定のタスクを実行し、リソースを管理できます。 グループ管理者、ユーザー管理者、その他のカスタム ロールなどのロールにユーザーを割り当てることができます。

Microsoft Entra PowerShell を使用してユーザーにロールを割り当てる方法については、「ユーザーへのロールの割り当て」を参照してください。

ユーザーの検索

displayNamemailNicknameuserPrincipalNamedepartmentjobTitleなどのさまざまな属性を使用して、組織内のユーザーを検索できます。 次の例は、次の方法でユーザーを検索する方法を示しています。 userPrincipalName

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "userPrincipalName eq 'SawyerM@contoso.com'"

出力には、 userPrincipalName 検索に基づいてユーザーの詳細が表示されます。

DisplayName      Id                                   Mail                 UserPrincipalName     
-----------      --                                   ----                 -----------------     
Sawyer Miller   aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb SawyerM@contoso.com  SawyerM@contoso.com   

特定の条件でユーザーを監査する

サインイン アクティビティやグループ メンバーシップなど、特定の条件でユーザーを監査できます。 この機能は、ユーザー アクティビティを追跡し、リソースへのアクセスを管理するのに役立ちます。

ユーザーのサインイン アクティビティを取得する

次の例は、特定のユーザーのサインイン アクティビティを取得する方法を示しています。

Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All'
Get-EntraUser -UserId 'SawyerM@contoso.com' -Property 'SignInActivity' | 
  Select-Object -Property Id, DisplayName, UserPrincipalName -ExpandProperty 'SignInActivity'

出力には、ユーザーのサインイン アクティビティが表示されます。

lastNonInteractiveSignInRequestId : bbbbbbbb-1111-2222-3333-aaaaaaaaaaaa
lastSignInRequestId               : cccccccc-2222-3333-4444-dddddddddddd
lastSuccessfulSignInDateTime      : 9/9/2024 1:12:13 PM
lastNonInteractiveSignInDateTime  : 9/9/2024 1:12:13 PM
lastSuccessfulSignInRequestId     : bbbbbbbb-1111-2222-3333-aaaaaaaaaaaa
lastSignInDateTime                : 9/7/2024 9:15:41 AM
id                                : aaaaaaaa-bbbb-cccc-1111-222222222222
displayName                       : Sawyer Miller
userPrincipalName                 : SawyerM@contoso.com

すべてのユーザーのサインイン アクティビティをダウンロードする

次の例では、ライセンスを取得したすべてのユーザー アカウントと、最後に成功したサインイン アクティビティを取得します。 さらに分析するために、データを CSV ファイルにエクスポートします。

# Connect to Microsoft Entra PowerShell  

Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All','Directory.Read.All'

try {
     Get-EntraUser -All -Property Id, UserPrincipalName, DisplayName, SignInActivity -ErrorAction Stop |
         Select-Object `
             Id, `
             UserPrincipalName, `
             DisplayName, `
             @{ Name = 'LastSignInDateTime';           Expression = { $_.SignInActivity.LastSignInDateTime } }, `
             @{ Name = 'LastSuccessfulSignInDateTime'; Expression = { $_.SignInActivity.LastSuccessfulSignInDateTime } } |
         Export-Csv -Path 'C:\temp\lastSignIns.csv' -NoTypeInformation -Encoding UTF8 -ErrorAction Stop

     Write-Host "Sign-in activity exported successfully to lastSignIns.csv"
 }
 catch {
     Write-Error "Failed to retrieve or export data: $_"
 }

この例では、組織内のすべてのユーザーの最後のサインイン日と最後に成功したサインイン日の両方を取得します。 データは、lastSignIns.csv ディレクトリ内の C:\temp という名前の CSV ファイルにエクスポートされます。

ユーザーのグループ メンバーシップを一覧表示する

次の例では、ユーザーがメンバーになっているグループの一覧を示します。

Connect-Entra -Scopes 'User.Read'
Get-EntraUserMembership -UserId 'SawyerM@contoso.com' |
 Select-Object Id, displayName, createdDateTime, '@odata.type' |
 Format-Table -AutoSize

出力には、ユーザーのメンバーシップが表示されます。

Id                                   displayName                         createdDateTime      @odata.type
--                                   -----------                         ---------------      -----------
00aa00aa-bb11-cc22-dd33-44ee44ee44ee Contoso                             2024-10-06T08:49:16Z #microsoft.graph.group
22cc22cc-dd33-ee44-ff55-66aa66aa66aa Contoso marketing                   2024-10-07T01:17:28Z #microsoft.graph.group
55ff55ff-aa66-bb77-cc88-99dd99dd99dd Pacific Admin Unit                                       #microsoft.graph.administrativeUnit

次のコマンドを使用して、ユーザーが属しているエンティティを一覧表示します。

ユーザーの上司、直属の部下を取得し、マネージャーをユーザーに割り当てる

  1. ユーザーのマネージャーを取得します。

    Connect-Entra -Scopes 'User.Read.All'
    Get-EntraUserManager -UserId 'SawyerM@contoso.com' |
        Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
        Format-Table -AutoSize
    

    出力には、ユーザーのマネージャーが表示されます。

    id                                    displayName     userPrincipalName                    createdDateTime           accountEnabled  userType
    --                                    -----------     -----------------                    ---------------           --------------  --------
    11bb11bb-cc22-dd33-ee44-55ff55ff55ff  Patti Fernandez PattiF@Contoso.com                 10/7/2024 12:32:01 AM      True           Member
    
  2. 特定のユーザーに報告するユーザーを一覧表示します。

    Connect-Entra -Scopes 'User.Read','User.Read.All'
    Get-EntraUserDirectReport -UserId 'SawyerM@contoso.com' |
        Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
        Format-Table -AutoSize
    

    出力には、ユーザーの直属のレポートが表示されます。

    id                                    displayName     userPrincipalName           createdDateTime       accountEnabled  userType
    --                                    -----------     -----------------           ---------------       --------------  --------
    bbbbbbbb-1111-2222-3333-cccccccccccc  Christie Cline  ChristieC@Contoso.com       10/7/2024 12:32:25 AM  True           Member
    aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb  Isaiah Langer   IsaiahL@Contoso.com         10/7/2024 12:33:16 AM  True           Member
    
  3. ユーザーにマネージャーを割り当てます。

    Connect-Entra -Scopes 'User.ReadWrite.All'
    Set-EntraUserManager -UserId 'SawyerM@contoso.com' -ManagerId 'AdeleV@contoso.com'
    
    • -UserId- Microsoft Entra IDのユーザーの ID (UserPrincipalName または User ObjectId として) を指定します。
    • -ManagerId - マネージャーとして割り当てる Microsoft Entra ID オブジェクトの UserPrincipalName またはユーザー ObjectId) として ID を指定します。

マネージャーなしでユーザーを一覧表示する

この例では、管理者がいないユーザーを一覧表示し、孤立したアカウント、サービス アカウント、または正しく構成されていないプロファイルを特定してクリーンアップできるようにします。

Connect-Entra -Scopes 'User.Read.All'
$allUsers = Get-EntraUser -All
$usersWithoutManagers = foreach ($user in $allUsers) {
    $manager = Get-EntraUserManager -UserId $user.Id -ErrorAction SilentlyContinue
    if (-not $manager) {
        [PSCustomObject]@{
            Id                = $user.Id
            DisplayName       = $user.DisplayName
            UserPrincipalName = $user.UserPrincipalName
            UserType          = $user.userType
            AccountEnabled    = $user.accountEnabled
            CreatedDateTime   = $user.createdDateTime
        }
    }
}
$usersWithoutManagers | Format-Table Id, DisplayName, UserPrincipalName, CreatedDateTime, UserType, AccountEnabled  -AutoSize

出力には、管理者がいないユーザーが一覧表示されます。

Id                                   DisplayName         UserPrincipalName                           CreatedDateTime           UserType   AccountEnabled
--                                   -----------         -----------------                           ---------------           --------   --------------
cccccccc-2222-3333-4444-dddddddddddd New User           NewUser@tenant.com                         10/7/2024 2:24:26 PM      Member     True
bbbbbbbb-1111-2222-3333-cccccccccccc Sawyer Miller     SawyerM@contoso.com                        10/7/2024 12:33:36 AM     Member     True

無効なユーザーを一覧表示する

次の例では、無効なアカウントの一覧を生成します。

Connect-Entra -Scopes 'User.ReadWrite.All'
Get-EntraUser -Filter "accountEnabled eq false" | Select-Object DisplayName, Id, Mail, UserPrincipalName

出力には、無効なユーザーが一覧表示されます。

DisplayName    Id                                   Mail userPrincipalName
-----------    --                                   ---- -----------------
Sawyer Miller  hhhhhhhh-7777-8888-9999-iiiiiiiiiiii      SawyerM@contoso.com
Kez Michael    eeeeeeee-4444-5555-6666-ffffffffffff      KezM@contoso.com