Edit

Share via


Manage users with Microsoft Entra PowerShell

Users are the representation of a Microsoft Entra work or school user account or a personal Microsoft account in Microsoft Entra ID. The user resource in Microsoft Entra PowerShell is the representation of a user, and includes relationships and resources that are relevant to the user.

The user resource provides a straightforward way for you to access and manipulate user resources without having to perform extra calls, look up specific authentication information, and directly issue queries against other Microsoft Entra PowerShell objects.

Prerequisites

To manage users with Microsoft Entra PowerShell, you need:

You can access a user's information and manage their data on their behalf or as an app with its own identity.

Onboard a user

To onboard a user, you create a new user account in Microsoft Entra ID. This process involves setting up the user's profile, including their display name, email address, and password.

Create a user

This example creates a new user.

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 = '[email protected]'
    AccountEnabled = $true
    MailNickName = 'NewUser'
}
New-EntraUser @userParams

The output displays details of the newly created user.

DisplayName    Id                                     Mail    UserPrincipalName
-----------    --                                     ----    -----------------
New User       aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb           [email protected]

Bulk create users

To create multiple users in bulk, you can use a CSV file. The CSV file should contain the necessary user attributes, such as DisplayName, UserPrincipalName, and PasswordProfile.

# 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
    }
FirstName LastName DisplayName EmailAddress Department MailNickName
Adele Vance Adele Vance [email protected] Marketing adelev

The output displays details of the newly created users.

DisplayName Id                                   Mail UserPrincipalName
----------- --                                   ---- -----------------
Adele Vance aaaaaaaa-1111-2222-3333-bbbbbbbbbbbb      [email protected]

Update a user's password

  1. To update a user's password by administrator, use this command:

    Connect-Entra -Scopes 'Directory.AccessAsUser.All'
    $newPassword = '<strong-password>'
    $securePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force
    Set-EntraUserPassword -UserId '[email protected]' -Password $securePassword
    
  2. To update the password for the signed-in user (self-serve), use this command:

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

    This command allows users to change their own passwords without admin privileges.

Upload or retrieve a photo for the user

  1. Upload a photo for a user.

    Connect-Entra -Scopes 'User.ReadWrite.All'
    Set-EntraUserThumbnailPhoto -UserId '[email protected]' -FilePath 'D:\UserThumbnailPhoto.jpg'
    

    This example sets the thumbnail photo of the user specified with the UserId parameter to the image specified with the FilePath parameter.

  2. Retrieve a user’s photo.

    Connect-Entra -Scopes 'ProfilePhoto.Read.All'
    Get-EntraUserThumbnailPhoto -UserId '[email protected]'
    

    This example demonstrates how to retrieve the thumbnail photo of a user that is specified through the value of the UserId parameter.

Grant users administrative roles in your organization

Granting users administrative roles in your organization allows them to perform specific tasks and manage resources. You can assign users to roles such as Group Administrator, User Administrator, or other custom roles.

To learn how to assign roles to users using Microsoft Entra PowerShell, see Assign roles to users.

Search users

Search for a user by mailNickname

To search for a user by mailNickname, use this command:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "startswith(MailNickname,'AdeleV')"

The output shows user details based on a mailNickname search.

DisplayName      Id                                   Mail                 UserPrincipalName     
-----------      --                                   ----                 -----------------     
Adell Vance      aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb [email protected]  [email protected]     

Search for a user by userPrincipalName

To search for a user by userPrincipalName, use this command:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "userPrincipalName eq '[email protected]'"

The output shows user details based on a userPrincipalName search.

DisplayName      Id                                   Mail                 UserPrincipalName     
-----------      --                                   ----                 -----------------     
Sawyer Miller   aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb [email protected]  [email protected]   

Search for users by job title

To search for users with the job title of Retail manager, use this command:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "jobTitle eq 'Retail Manager'"

The output shows user details based on a jobTitle search.

DisplayName      Id                                   Mail                 UserPrincipalName     
-----------      --                                   ----                 -----------------     
Sawyer Miller   aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb [email protected]  [email protected]   

Search for users in Marketing department

To search for users in Marketing department, use this command:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "department eq 'Marketing'"

The output shows user details based on a department search.

DisplayName     Id                                   Mail                          UserPrincipalName     
-----------     --                                   ----                          -----------------     
Adell Vance     aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb [email protected]           [email protected]     
Christie Cline  bbbbbbbb-1111-2222-3333-cccccccccccc [email protected]        [email protected]  

Search for users by most recently created

To find the five most recently created users, use this command:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -All | Sort-Object -Property createdDateTime -Descending | Select-Object -First 5

The output lists recently created users.

DisplayName     Id                                   Mail                          UserPrincipalName     
-----------     --                                   ----                          -----------------     
Adell Vance     aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb [email protected]           [email protected]     
Christie Cline  bbbbbbbb-1111-2222-3333-cccccccccccc [email protected]        [email protected]  
Sawyer Miller   cccccccc-2222-3333-4444-dddddddddddd [email protected]          [email protected]    
Kez Michael     hhhhhhhh-7777-8888-9999-iiiiiiiiiiii [email protected]             [email protected]       
Avery Smith     eeeeeeee-4444-5555-6666-ffffffffffff [email protected]           [email protected]     

Audit users by certain criteria

You can audit users by certain criteria, such as sign-in activity, and group memberships. This feature helps you track user activity and manage their access to resources.

Retrieve a user's sign-in activity

The following example shows how to retrieve the sign-in activity of a specific user.

Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All'
Get-EntraUser -UserId '[email protected]' -Property 'SignInActivity' | 
  Select-Object -Property Id, DisplayName, UserPrincipalName -ExpandProperty 'SignInActivity'

The output shows the user's sign-in activity.

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                 : [email protected]

List a user's group memberships

The following example lists the groups that a user is a member of.

Connect-Entra -Scopes 'User.Read'
Get-EntraUserMembership -UserId '[email protected]' |
 Select-Object Id, displayName, createdDateTime, '@odata.type' |
 Format-Table -AutoSize

The output shows the user's memberships.

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

Use these commands to list the entities a user belongs to:

Get a user's manager, direct reports and assign a manager to a user

  1. Get a user's manager.

    Connect-Entra -Scopes 'User.Read.All'
    Get-EntraUserManager -UserId '[email protected]' |
        Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
        Format-Table -AutoSize
    

    The output shows the user's manager.

    id                                    displayName     userPrincipalName                    createdDateTime           accountEnabled  userType
    --                                    -----------     -----------------                    ---------------           --------------  --------
    11bb11bb-cc22-dd33-ee44-55ff55ff55ff  Patti Fernandez [email protected]                 10/7/2024 12:32:01 AM      True           Member
    
  2. List the users who report to a specific user.

    Connect-Entra -Scopes 'User.Read','User.Read.All'
    Get-EntraUserDirectReport -UserId '[email protected]' |
        Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
        Format-Table -AutoSize
    

    The output shows the user's direct report.

    id                                    displayName     userPrincipalName           createdDateTime       accountEnabled  userType
    --                                    -----------     -----------------           ---------------       --------------  --------
    bbbbbbbb-1111-2222-3333-cccccccccccc  Christie Cline  [email protected]       10/7/2024 12:32:25 AM  True           Member
    aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb  Isaiah Langer   [email protected]         10/7/2024 12:33:16 AM  True           Member
    
  3. Assign a manager to a user.

    Connect-Entra -Scopes 'User.ReadWrite.All'
    Set-EntraUserManager -UserId '[email protected]' -ManagerId '[email protected]'
    
    • -UserId - specifies the ID (as a UserPrincipalName or User ObjectId) of a user in Microsoft Entra ID.
    • -ManagerId - specifies the ID as a UserPrincipalName or User ObjectId) of the Microsoft Entra ID object to assign as a manager.

List users without managers

This example lists users without a manager, helping to identify orphaned accounts, service accounts, or misconfigured profiles for cleanup.

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

The output lists users without managers.

Id                                   DisplayName         UserPrincipalName                           CreatedDateTime           UserType   AccountEnabled
--                                   -----------         -----------------                           ---------------           --------   --------------
cccccccc-2222-3333-4444-dddddddddddd New User           [email protected]                         10/7/2024 2:24:26 PM      Member     True
bbbbbbbb-1111-2222-3333-cccccccccccc Sawyer Miller     [email protected]                        10/7/2024 12:33:36 AM     Member     True

List disabled users

The following example generates a list of disabled accounts.

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

The output lists disabled users.

DisplayName    Id                                   Mail userPrincipalName
-----------    --                                   ---- -----------------
Sawyer Miller  hhhhhhhh-7777-8888-9999-iiiiiiiiiiii      [email protected]
Kez Michael    eeeeeeee-4444-5555-6666-ffffffffffff      [email protected]