Powershell code to disable a windows account in azure ad
- User
Powershell code to disable a windows account in azure ad
- Assistant
Powershell code to disable a windows account in azure ad
Certainly! To disable a Windows user account in Azure Active Directory (Azure AD) using PowerShell, you can use the AzureAD module. Below are the steps and the code to accomplish this:
Install the AzureAD Module: Ensure you have the AzureAD PowerShell module installed. If not, you can install it using the following command:
Install-Module -Name AzureAD
Permissions: You must have the necessary administrative privileges to manage user accounts in Azure AD.
Connect to Azure AD: Authenticate your PowerShell session with Azure AD.
Disable the User Account: Set the AccountEnabled property of the user to $false.
Here is the PowerShell script to disable a user account:
# Import the AzureAD module
Import-Module AzureAD
# Connect to Azure AD
Connect-AzureAD
# Specify the User Principal Name (email) or Object ID of the user to disable
$userId = "user@example.com" # Replace with the user's UPN or Object ID
# Disable the user account
Set-AzureADUser -ObjectId $userId -AccountEnabled $false
# Confirm the change
$user = Get-AzureADUser -ObjectId $userId
Write-Host "The account for $($user.DisplayName) is enabled: $($user.AccountEnabled)"
"user@example.com" with the actual UPN or Object ID.AccountEnabled property to $false, effectively disabling the account.Re-enabling the Account: To enable the account again, set the AccountEnabled property to $true:
Set-AzureADUser -ObjectId $userId -AccountEnabled $true
Using Object ID: If you prefer to use the user's Object ID instead of the UPN:
# Get the user's Object ID
$user = Get-AzureADUser -Filter "UserPrincipalName eq '$userId'"
$objectId = $user.ObjectId
# Disable the account
Set-AzureADUser -ObjectId $objectId -AccountEnabled $false
Microsoft recommends using the Microsoft Graph PowerShell SDK for interacting with Azure AD. Here's how you can do it:
Install the Microsoft Graph Module:
Install-Module Microsoft.Graph
# Import the Microsoft Graph Users module
Import-Module Microsoft.Graph.Users
# Connect to Microsoft Graph with the necessary scope
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Specify the User Principal Name (email) or ID of the user to disable
$userId = "user@example.com" # Replace with the user's UPN or ID
# Disable the user account
Update-MgUser -UserId $userId -AccountEnabled:$false
# Confirm the change
$user = Get-MgUser -UserId $userId -Property AccountEnabled, DisplayName
Write-Host "The account for $($user.DisplayName) is enabled: $($user.AccountEnabled)"
By running the above scripts, you can disable a user account in Azure AD using PowerShell. Remember to replace placeholders with actual values specific to your environment, and ensure you have the necessary permissions to perform this action.
Not yet included in a published release.