Skip to content

Create Microsoft Entra ID Users from CSV with PowerShell

We like to create new Microsoft Entra ID users in the company. Going through the Microsoft Entra admin center and creating the users through the wizard will take us time. To speed things up, we will choose PowerShell to create bulk Microsoft Entra ID users. In this article, you will learn how to create users in Microsoft Entra ID from CSV file.

Connect to Microsoft Graph

Before you start, you need to Install and connect to Microsoft Graph.

Start Windows PowerShell as administrator and run the below commands.

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

Important: Always install the Microsoft Graph PowerShell and Microsoft Graph Beta PowerShell modules. That’s because some cmdlets are not yet available in the final version, and they will not work. Update both modules to the latest version before you run a cmdlet or script to prevent errors and incorrect results.

Run the Connect-MgGraph cmdlet to initiate a connection with Microsoft Entra ID.

Connect-MgGraph -Scopes "User.ReadWrite.All"

Required attributes to create a Microsoft Entra ID user

To create a Microsoft Entra ID user with PowerShell, you need the minimum required attributes:

  • -DisplayName
  • -MailNickname
  • -UserPrincipalName
  • -PasswordProfile
  • -AccountEnabled

Create Microsoft Entra ID user with PowerShell

Start PowerShell ISE or Visual Studio Code and run the New-MgUser cmdlet to create a new Microsoft Entra ID user.

# Create password profile
$PasswordProfile = @{
    Password                             = "P@ssw0rd!"
    ForceChangePasswordNextSignIn        = $true
    ForceChangePasswordNextSignInWithMfa = $true
}

# Create Microsoft Entra ID user
$UserParams = @{
    DisplayName       = "Jeff Baker"
    MailNickName      = "Jeff.Baker"
    UserPrincipalName = "Jeff.Baker@exoip.com"
    PasswordProfile   = $PasswordProfile
    AccountEnabled    = $true
}

New-MgUser @UserParams

After running the command, it will show the output that the user is successfully created.

DisplayName Id                                   Mail UserPrincipalName
----------- --                                   ---- -----------------
Jeff Baker  4cc4f605-8ed1-47f5-8c5f-ca9a29e8ec82      Jeff.Baker@exoip.com

Now that you know how to create a Microsoft Entra ID user with Microsoft Graph PowerShell, let’s look at the next step how to bulk create Microsoft Entra ID users with a CSV file.

Create bulk Microsoft Entra ID users with PowerShell

Let’s go through the steps to create bulk Microsoft Entra ID users from CSV file.

1. Download CSV template

Download NewUsers.csv and place the CSV template in path C:\Temp. If you don’t have a Temp folder, create one.

Adjust the CSV file and save it when done.

Create Microsoft Entra ID users from CSV file template

2. Download Add-NewEntraIDUsers PowerShell script

Download the Add-NewEntraIDUsers.ps1 script and save it in path C:\Scripts. If you don’t have a Scripts folder, create one.

Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code into Notepad. Give it the name Add-NewEntraIDUsers.ps1 and place it in the C:\Scripts folder.

<#
    .SYNOPSIS
    Add-NewEntraIDUsers.ps1

    .DESCRIPTION
    Create Microsoft Entra ID users from CSV file.

    .LINK
    www.alitajran.com/create-microsoft-entra-id-users

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 05/19/2023 - Initial version
#>

# Connect to Microsoft Graph with user read/write permissions
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Specify the path of the CSV file
$CSVFilePath = "C:\temp\NewUsers.csv"

# Create password profile
$PasswordProfile = @{
    Password                             = "P@ssw0rd!"
    ForceChangePasswordNextSignIn        = $true
    ForceChangePasswordNextSignInWithMfa = $true
}

# Import data from CSV file
$Users = Import-Csv -Path $CSVFilePath

# Loop through each row containing user details in the CSV file
foreach ($User in $Users) {
    $UserParams = @{
        DisplayName       = $User.DisplayName
        MailNickName      = $User.MailNickName
        UserPrincipalName = $User.UserPrincipalName
        Department        = $User.Department
        JobTitle          = $User.JobTitle
        Mobile            = $User.Mobile
        Country           = $User.Country
        EmployeeId        = $User.EmployeeId
        PasswordProfile   = $PasswordProfile
        AccountEnabled    = $true
    }

    try {
        $null = New-MgUser @UserParams -ErrorAction Stop
        Write-Host ("Successfully created the account for {0}" -f $User.DisplayName) -ForegroundColor Green
    }
    catch {
        Write-Host ("Failed to create the account for {0}. Error: {1}" -f $User.DisplayName, $_.Exception.Message) -ForegroundColor Red
    }
}

3. Run the Add-NewEntraIDUsers PowerShell script

Change the directory path to C:\scripts and run the script Add-NewEntraIDUsers.ps1.

C:\scripts\.\Add-NewEntraIDUsers.ps1

The output will show whether the users were successfully or unsuccessfully created with a message.

Welcome To Microsoft Graph!
Successfully created the account for Amanda Morgan
Successfully created the account for Jeffrey Welch
Successfully created the account for Kevin Howard
Failed to create the account for Lauren Russell. Error: Another object with the same value for property userPrincipalName already exists.

4. Verify Microsoft Entra ID users

Run the script below that uses the Get-MgUser cmdlet to retrieve the newly created users in Microsoft Entra ID.

# Specify the path of the CSV file
$CSVFilePath = "C:\temp\NewUsers.csv"

# Import the CSV file
$users = Import-Csv $CSVFilePath

foreach ($user in $users) {
    if (Get-MgUser -UserId $user.UserPrincipalName -ErrorAction SilentlyContinue) {
        Write-Host "$($user.UserPrincipalName) exists" -ForegroundColor Green
    }
    else {
        Write-Host "$($user.UserPrincipalName) does not exist" -ForegroundColor Red
    }
}

Another way is to sign in to the Microsoft Entra admin center and verify the newly created users.

Create Microsoft Entra ID users from CSV file admin center

When the users sign in to Microsoft 365, they are forced to update their password.

Create Microsoft Entra ID users from CSV file update password

Did this help you to create Microsoft Entra ID Users from CSV file with PowerShell?

Note: Suppose you don’t want to use PowerShell and like to create the users through the Microsoft Entra admin center; read the article Bulk create Microsoft 365 users with CSV file.

Read more: Get MFA status in Microsoft Entra and PowerShell »

Conclusion

You learned how to create Microsoft Entra ID users from CSV with PowerShell. First, prepare the CSV file and ensure all the information is filled in. Next, run the Add-NewEntraIDUsers.ps1 script to create the users in Microsoft Entra ID. The final part is to check if the users are created successfully.

Did you enjoy this article? You may also like Prevent MFA fatigue attacks in organization. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 3 Comments

  1. Just followed this guide to successfully import a bunch of users for our cross-tenant migration. Thanks, much appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *