Remove-MsolUser

In this Azure PowerShell article, we will discuss the syntax and usage of the Remove-MsolUser Azure PowerShell command with an example of how to use this command.

Remove-MsolUser

This PowerShell command can help you to remove a user from your Azure Active Directory.

Syntax

Below is the syntax of the Remove-MsolUser PowerShell command.

Remove-MsolUser -UserPrincipalName "Your User Principal Name "

Let’s see an example of how to use the Remove-MsolUser PowerShell command.

Example

For example, I have a user in my Azure Active Directory with the user principal name “test@fewlines4bijuhotmail.onmicrosoft.com”. You can execute the below script to delete the specific user.

Remove-MsolUser -UserPrincipalName "test@fewlines4bijuhotmail.onmicrosoft.com"

After executing the above script, I got a confirmation pop-up –> Just clicked on the Yes button to confirm the deletion process.

delete azure ad user powershell

Ohh, I got the below error

“Remove-MsolUser : You must call the Connect-MsolService cmdlet before calling any other cmdlets.”

The complete error message was

Remove-MsolUser : You must call the Connect-MsolService cmdlet before calling any 
other cmdlets.
At line:1 char:1
+ Remove-MsolUser -UserPrincipalName "test@fewlines4bijuhotmail.onmicro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Remove-MsolUser], MicrosoftOnlin 
   eException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MicrosoftOnl 
   ineException,Microsoft.Online.Administration.Automation.RemoveUser

You can see the error message here.

You must call the Connect-MsolService cmdlet before calling any other cmdlets

This error message is because we have not established any connection with the Azure AD, so the process is that we should establish a connection with the Azure Active Directory with the help of the Connect-MsolService cmdlet. Then, you can delete the user from the Azure Active Directory using the above PowerShell script.

So to solve the error “You must call the Connect-MsolService cmdlet before calling any other cmdlets,” first run the Connect-MsolService Azure PowerShell cmdlet, and then you can run the other script.

Run the script like below

Connect-MsolService
Remove-MsolUser -UserPrincipalName "test@fewlines4bijuhotmail.onmicrosoft.com"

After executing the above script, you will be redirected to enter your credentials. Then, within seconds, the user will be deleted.

Suppose you don’t want the confirmation pop-up and want to delete the user directly. Then, you can use the script below.

You may also like following the articles below

Conclusion

You can use the Remove-MsolUser PowerShell command to delete a user from your Azure Active Directory. In this article, we discussed the syntax and usage of the Remove-MsolUser PowerShell command with an example of how to use this command.