Forum Discussion
Ajay_Kumar_Ghose
Jun 21, 2023Brass Contributor
Need a PowerShell Script to add multiple users to Azure Ad using csv file.
Script that i have tried its showing the below error. And Unable to fix it. Myscript:- #Import-Module AzureAD #Already installed # Set the path to your CSV file $csvPath = "C:\Users\pathto\newus...
- 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.
Ajay_Kumar_Ghose
Jun 21, 2023Brass Contributor
Hello eliekarkafy, Thank you for your reply.
Yes that's correct. I can able to add the csv file manually using portal. But our requirement is to automate using PowerShell script. Could you please help me on the same. Thank you
Yes that's correct. I can able to add the csv file manually using portal. But our requirement is to automate using PowerShell script. Could you please help me on the same. Thank you
eliekarkafy
Jun 21, 2023MVP
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,Password2
Save 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-AzureAD
This 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.
- Ajay_Kumar_GhoseJun 22, 2023Brass ContributorThank you so much , It helped . Just need to change this ForceChangePasswordNextSignIn to ForceChangePasswordNextLogin. And remove extra whitespace if there is any.
- Ajay_Kumar_GhoseJul 27, 2023Brass ContributorHello , eliekarkafy, To a follow-up on this, If i want to add those users to a particular group in azure ad. How can i proceed with this. TIA
- eliekarkafyJul 27, 2023MVPif you have the users in a CSV file you can import them from the Entra ID portal directly without using Powershell
- Ajay_Kumar_GhoseJun 21, 2023Brass ContributorLet me try in this way as well, Does version no required in the excel . as suggested MS DOCUMENT by you.
- eliekarkafyJun 21, 2023MVPPlease click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily.
- Ajay_Kumar_GhoseJun 22, 2023Brass Contributor
Hello eliekarkafy, when i am trying to execute the following command getting this error
New-AzureADUser : Cannot bind parameter 'PasswordProfile'. Cannot create object of type "Microsoft.Open.AzureAD.Model.PasswordProfile". The ForceChangePasswordNextSignIn property
was not found for the Microsoft.Open.AzureAD.Model.PasswordProfile object. The available property is: [Password <System.String>] , [ForceChangePasswordNextLogin
<System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]>] , [EnforceChangePasswordPolicy
<System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]>]
At line:21 char:17
+ New-AzureADUser @userParams
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-AzureADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Open.AzureAD16.PowerShell.NewUser
My CSV File looks like this:-
Username FirstName LastName Password
Ommkar16 omm kar16 Ajay#789
akarmurti akar murti Ajay#788
I haven't set UPN as its in script which can be added. Please let me know if you required any further information. Could you please help me on the same. Thanks. Or else let me know if something i am missing
- Ajay_Kumar_GhoseJun 21, 2023Brass ContributorThanks Let me check again, Currently I am using the csv file by download from Azure ad bulk operation. And edit according by requirement. Does that file not work