Azure AD PowerShell is a powerful tool for Azure administrators, allowing them to manage users, groups, and applications with ease.
To get started with Azure AD PowerShell, you'll need to install the Azure Active Directory module, which can be done using the Install-Module cmdlet.
The Azure AD PowerShell module provides a set of cmdlets that make it easy to perform common tasks, such as creating and managing users, groups, and applications.
For example, you can use the New-AzureADUser cmdlet to create a new user, and then use the Set-AzureADUser cmdlet to configure their properties.
Getting Started
Azure AD PowerShell is a powerful tool for managing your Azure Active Directory.
First, you'll need to install the Azure AD PowerShell module, which can be done using the PowerShell Gallery.
You can install the module by running the command "Install-Module -Name AzureAD" in your PowerShell terminal.
This will allow you to connect to your Azure AD tenant and start managing your users, groups, and applications.
Before you start, make sure you have the necessary permissions to manage your Azure AD tenant.
You can check your permissions by running the command "Get-AzureADDirectorySetting | Select-Object -ExpandProperty "AllowUserToRequestDirectoryExtensions"" in your PowerShell terminal.
If you don't have the necessary permissions, you'll need to work with your Azure AD administrator to obtain the required permissions.
Once you have the necessary permissions, you can start exploring the Azure AD PowerShell module and its many features.
Azure AD PowerShell Commands
To connect to Azure Active Directory, you'll need to use the Connect-AzureAD PowerShell cmdlet. This cmdlet helps you connect Azure Active Directory with an authenticated account.
You can use the Disconnect-AzureAD cmdlet to disconnect the current logged-in session from Azure Active Directory. This is useful if you need to switch between different accounts or sessions.
To fetch information about Azure Active Directory roles, use the Get-AzureADDirectoryRole cmdlet. This cmdlet allows you to fetch the details of Azure Active Directory roles.
You can also use the Get-AzureADUser cmdlet to fetch information about users in your Azure Active Directory tenant. This cmdlet helps you to fetch the information of users in Azure Active Directory tenant.
The Azure AD Administrator role or a custom AD role with the Service Read permission is required to fetch the information or details of any Azure AD services.
Here are some PowerShell cmdlets that can help you manage Azure Active Directory:
- Connect-AzureAD: Connects Azure Active Directory with an authenticated account.
- Disconnect-AzureAD: Disconnects the current logged-in session from Azure Active Directory.
- Get-AzureADDirectoryRole: Fetches the details of Azure Active Directory roles.
- Get-AzureADUser: Fetches the information of users in Azure Active Directory tenant.
- Get-AzureADGroup: Fetches the information of Azure AD Group in the tenant.
Managing Azure AD
Managing Azure AD is a crucial task for sysadmins, and PowerShell makes it a breeze. With the right commands, you can automate various tasks and perform administrative duties with ease.
You can use the Get-PrivilegedRoleAssignment command to list any role assignments, permanent or eligible, your user might have. This command will return a role assignment variable that you can use in subsequent commands.
Here are some examples of role assignments for two different admin users:
With this information, you can use the Enable-PrivilegedRoleAssignment command to enable one of your roles, specifying a duration and the role either by RoleId or RoleAssignment variable.
Disconnect-Pim Service
Disconnecting from Azure AD PIM Service is a straightforward process. To end your connection, run the command Disconnect-PimService.
After running this command, you'll notice that there are no role assignments to list anymore. This indicates that your connection has been successfully terminated.
Manage with Examples
Managing Azure AD with PowerShell can be a breeze with the right commands and examples.
To disconnect from the Azure AD PIM Service, you can simply run the command Disconnect-PimService.
Here are some common tasks and the commands you need to get them done.
To list any role assignments, permanent or eligible, you can use the command Get-PrivilegedRoleAssignment.
This command will return a list of your role assignments, and you can use it to enable or disable roles.
To enable a role, you need to specify a duration and which role to enable. You can use the command Enable-PrivilegedRoleAssignment, and you can specify the role by RoleId or RoleAssignment variable.
Here's an example of how to enable a role: Enable-PrivilegedRoleAssignment -RoleId "Privileged Role Administrator" -Duration 1 -Reason "Performing administrative tasks".
To deactivate a role, you can use the command Disable-PrivilegedRoleAssignment, specifying a RoleId or RoleAssignment variable.
If you have already activated a role, it will automatically deactivate after the specified duration. However, you can also deactivate it manually if you're finished with administrative tasks.
Here are some examples of how to use these commands:
Note that if you have required MFA on activation for the role, you may be prompted to verify your identity when activating the role.
Environmental Cleanup
This command will successfully remove the module without any errors in the PowerShell terminal.
To verify the removal of the module, use the command: Get-Module -Name AzureAD
If the module is successfully removed, you won't get any errors.
User Management
User Management with Azure AD PowerShell is a breeze. You can get information about Azure AD users with the "Get-AzureADUser" cmdlet, which returns all users in the Azure Active Directory environment by default.
The cmdlet returns the users' ObjectId, DisplayName, and UserPrincipalName. You can also pipe the command to the Get-Member cmdlet to see all available properties and methods.
To troubleshoot Azure AD sync issues, you can use the "Get-AzureADUser" cmdlet with specific properties such as DirSyncEnabled and LastDirSyncTime. This can be especially helpful if you have already set up Azure AD Connect.
Here are some common properties you can return using the "Get-AzureADUser" cmdlet:
- DisplayName
- UserPrincipalName
- DirSyncEnabled
- LastDirSyncTime
You can also create new Azure AD users using the "New-AzureADUser" cmdlet, which is particularly useful when creating multiple users from a CSV file.
Privileged Role Assignment
Privileged Role Assignment is a powerful feature that allows administrators to delegate specific tasks to other users. This feature is especially useful when you need to grant temporary access to sensitive areas of your system.
To view any role assignments for a user, you can use the Get-PrivilegedRoleAssignment command. This will list any permanent or eligible role assignments for the user. For example, a user might be eligible for Security Administrator and Privileged Role Administrator, and permanent for Global Administrator.
The Get-PrivilegedRoleAssignment command returns a role assignment variable that can be used in subsequent commands. This variable can be used to enable or disable specific roles.
To enable a role, you'll need to specify a duration and the role you want to enable. You can do this using the Enable-PrivilegedRoleAssignment command. This command takes a role assignment variable as an argument, along with the duration and any optional parameters like Reason.
Here's an example of how to enable a role:
After enabling a role, you can verify that it has been activated by running the Get-PrivilegedRoleAssignment command again. The role will be listed as active, along with the expiration time.
It's worth noting that if you have required MFA on activation for the role, you may be prompted to verify your identity when activating the role. This is the same experience as activating roles through the Azure Portal.
Get User Information with Get-User Cmdlet
You can get information about Azure AD users with the Get-AzureADUser cmdlet. This cmdlet returns the users' ObjectId, DisplayName, and UserPrincipalName by default.
To see all available properties, pipe the Get-AzureADUser command to the Get-Member cmdlet. This will display a list of properties and methods.
Here are some properties you can return with the Get-AzureADUser cmdlet:
- DisplayName
- UserPrincipalName
- DirSyncEnabled
- LastDirSyncTime
You can modify the Get-AzureADUser command to return more customized results by selecting specific properties.
For example, to get the DisplayName, UserPrincipalName, DirSyncEnabled, and LastDirSyncTime properties for all users, use the following command:
Get-AzureADUser -All | Select-Object -Property DisplayName, UserPrincipalName, DirSyncEnabled, LastDirSyncTime
You can also use the Get-MgUser cmdlet to get information about Azure AD users. This cmdlet returns the User's Id, DisplayName, Mail, and UserPrincipalName properties by default.
To get a single Azure user, use the -UserID parameter. For example:
Get-MgUser -UserID [email protected]
To get all Azure users and export them to a CSV file, use the following command:
Get-MgUser -All | Select-Object -Property displayName, id, UserPrincipalName | Export-Csv "C:\temp\GetAzureAdUsers.csv"
Group Membership
Adding users to a group is a straightforward process. To do this, you'll need to use the Add-AzureADGroupMember cmdlet, which requires the ObjectID of both the user and the group.
You can obtain the ObjectID of the group using the Get-AzureADMSGroup command. This command returns the ID and DisplayName of the group, so be sure to replace "Office 365 sample group" with your actual group's name.
To get the ObjectID of the user, you'll use the Get-AzureADUser command, which returns the ObjectID and DisplayName of the user. Simply replace "[email protected]" with your user's UPN.
Once you have both ObjectIDs, you can add the user to the group using the Add-AzureADGroupMember cmdlet.
Frequently Asked Questions
How do I run PowerShell on AzureAD?
To run PowerShell on Azure AD, install the Azure AD PowerShell Module and connect to Azure AD using the Connect-AzureAD cmdlet. This will enable you to start using cmdlets from the Azure AD PowerShell Module.
Is the AzureAD PowerShell module deprecated?
Yes, the AzureAD PowerShell module is deprecated as of March 30, 2024, with support limited to critical security fixes. It will continue to function until March 30, 2025.
How to download AzureAD PowerShell module?
To download the AzureAD PowerShell module, open the Start menu, search for 'Powershell', and run it as administrator. Then, type the command to install the NuGet provider and follow the prompts to complete the download.
What replaced MSOnline?
Microsoft Graph PowerShell SDK has replaced MSOnline as the recommended PowerShell provider for Microsoft Entra. Learn more about the transition and how to migrate to the new SDK.
Sources
- https://gotoguy.blog/2018/05/22/getting-started-with-azure-ad-pim-powershell-module/
- https://lazyadmin.nl/powershell/install-azure-ad-module/
- https://infrasos.com/connect-azuread-how-to-connect-to-azure-ad-using-powershell/
- https://www.geeksforgeeks.org/how-to-install-and-connect-to-azure-ad-with-powershell/
- https://www.lepide.com/how-to/powershell-to-get-and-export-azure-ad-users-to-csv.html
Featured Images: pexels.com