Forum Discussion
Need a PowerShell Script to add multiple users to Azure Ad using csv file.
- Jun 21, 2023
Step 1: Prepare the CSV File Create a CSV file with the necessary user information. The CSV file should contain columns with headers such as "Username," "FirstName," "LastName," "Password," and any additional attributes you want to include. Here's an example:
Username,FirstName,LastName,Password
user1,John,Doe,Password1
user2,Jane,Smith,Password2Save the file as "users.csv" or any name you prefer.
Step 2: Connect to Azure AD Open PowerShell and install the AzureAD module if you haven't already. Run the following command:
Install-Module -Name AzureAD
Then, connect to Azure AD using the following commands:
Import-Module AzureAD
Connect-AzureADThis will prompt you to sign in to your Azure AD account.
Step 3: Import and Create Users Now, import the CSV file and loop through each row to create users. Here's an example script:
# Import CSV file
$users = Import-Csv -Path "C:\Path\to\users.csv"# Loop through each row and create users
foreach ($user in $users) {
$username = $user.Username
$firstName = $user.FirstName
$lastName = $user.LastName
$password = $user.Password# Create user
$userParams = @{
UserPrincipalName = $username + '@yourdomain.com'
DisplayName = $firstName + ' ' + $lastName
GivenName = $firstName
Surname = $lastName
PasswordProfile = @{
Password = $password
ForceChangePasswordNextSignIn = $true
}
}New-AzureADUser @userParams
}Make sure to replace "C:\Path\to\users.csv" with the actual path to your CSV file.
Step 4: Run the Script Save the PowerShell script with a .ps1 extension, for example, "create-users.ps1". Open PowerShell, navigate to the script's location, and run the script by executing the following command:
.\create-users.ps1
The script will read the CSV file and create users in Azure AD based on the provided information.
The error is telling you that you've incorrectly typed "ForceChangePasswordNextSignIn" when the expected parameter name is "ForceChangePasswordNextLogin".
Cheers,
Lain
AccountEnabled: true
New-AzureADUser : Error occurred while executing NewUser
Code: Request_BadRequest
Message: Property mailNickname value is required but is empty or missing.
RequestId: fbe7bd00-78d9-472c-81ea-016594a0eb35
DateTimeStamp: Thu, 22 Jun 2023 06:22:15 GMT
Details: PropertyName - mailNickname, PropertyErrorCode - PropertyRequired
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At C:\Users\ajay.ghose\testdevAzure\DevDemoAjay-2\UsercreateMS.ps1 could you please check once. TIA
- Ajay_Kumar_GhoseJun 22, 2023Brass ContributorThank you , Its resolved after change to this "ForceChangePasswordNextLogin" and there was whitespace in mailNickname so unable to proceed now its working fine.
- LainRobertsonJun 22, 2023Silver Contributor
Yes, so long as the data inside your CSV is valid and you have the necessary permissions to add people to Azure AD, you should find your script continues to work.
Cheers,
Lain
- Ajay_Kumar_GhoseJun 22, 2023Brass ContributorThank you Lain, For your valuable inputs . To know about this will i be able to upload same csv file by adding more users
- LainRobertsonJun 22, 2023Silver Contributor
The error spells out the issue quite clearly:
Message: Property mailNickname value is required but is empty or missing.
You need to include a value for mailNickname. See the example from Microsoft's documentation:
Given you are using a hash table, you may prefer to include it there instead of via the "-MailNickname" parameter.
Cheers,
Lain