Friday, August 23, 2019

Create a user in Azure Active Directory using PowerShell (Windows 10)

I recently tried to create an user in Azure Active Directory using the Windows 10 PowerShell application and run through several issues, so I published this post to help people running through the same troubles.

Microsoft documentation reference :

  • Tutorial: Grant a user access to Azure resources using RBAC and Azure PowerShell
    The above link gives you an example of using PowerShell to create a user in Azure Active Diretory but it uses the Azure Cloud Shell. If you want to create a PowerShell script that you want to reuse from time to time to create users in Azure AD in a large company, using Azure Cloud Shell is not the best solution.
  • New-AzureADUser
    The above link gives you an example of using PowerShell to create a user in Azure Active Diretory but not explain the trick to configure your PowerShell environment properly to make it work.

Tutorial

(tricks to create a user in Azure Active Directory using Windows 10 PowerShell Application)

1. Prerequisites

For doing this tutorial you need to have:
  • An Azure tenant
  • Permissions to create users in the Azure Active Directory of this tenant.
  • Azure PowerShell Az module installed

2. Installation of AzureAD PowerShell Module

Open the Windows 10 PowerShell Application.
Type PowerShell in the Windows 10 pane and right click the Windows 10 PowerShell Application icon and open it "as Administrator"



In the PowerShell command type:
Install-Module AzureAD
to install the PowerShell Azure AD Module.


You can then check that the AzureAD Powershell Module has been installed successfully by typing the following command:
Get-InstalledModule


and most of all that the module can be actually used:
Get-Module -Listavailable


then go to the directory where the module was installed to be able to copy the path and the name of the dll we need later.
It should be:
C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.31\Microsoft.Open.AzureAD16.Graph.Client.dll


3. Loading the Microsoft.Open.AzureAD16.Graph.Client.dll within PowerShell Application.

Now, we have to load the dll within PowerShell. If we don't we will get an issue in PowerShell while trying to create the variable for the password of the user:
New-Object : Cannot find type [Microsoft.Open.AzureAD.Model.PasswordProfile]: make sure 
the assembly containing this type is loaded.
So let's load the dll in PowerShell by typing the following instruction with the path copied previously:
Add-Type -Path 'C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.31\Microsoft.Open.AzureAD16.Graph.Client.dll'

4. Creating the user in Azure AD

Then let's create the password variable. You can use P@ssw0rd that is compliant to the required rules for an Azure AD password:
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = "P@ssw0rd"
Then we have to connect to Azure AD in order to really create the user. Type:
Connect-AzureAD
A dialog is opening for you to type your Azure Account Login


then your Azure Account password


You should have the following screen after the connection:


Then type the following line by replacing the tenant domain of the User Principal Name by your tenant domain (mine is charmoisdev.onmicrosoft.com):
New-AzureADUser -DisplayName "RBAC Tutorial User2" -PasswordProfile $PasswordProfile `
-UserPrincipalName "rbacuser2@charmoisdev.onmicrosoft.com" -AccountEnabled $true -MailNickName "rbacuser2"
You sould have the following screen after user creation:


You can then check in the Azure Portal that your user has been created successfully:



4 comments:

Adrian Bole said...

Thanks for this - I had exactly the same issue and had to load the dll. I was a bit slow and didn't realise the path where the modules were installed was listed in the output above all the modules themselves!

Salwa Harif said...

Thank You very much !
I had the same error and wasn't sure which module to load :)

Dhanya said...

This helped me resolve the issue.Thank you

Marc Charmois said...

Hi Dhanya,
Happy to hear that, happy to help :-)

Marc