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\newuserscript.csv"
# Set the desired password length
$passwordLength = 10
# Read the CSV file
$users = Import-Csv -Path $csvPath
# Connect to Azure AD
#Connect-AzureAD
# Loop through each user in the CSV file
foreach ($user in $users) {
# Generate a random password
$password = [System.Web.Security.Membership]::GeneratePassword($passwordLength, 2)
# Create a new user object
$newUserParams = @{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
PasswordProfile = @{
Password = $password
ForceChangePasswordNextSignIn = $true
}
}
# Add the user to Azure AD
New-AzureADUser @newUserParams
# Output the user details and password to the console
Write-Output "User created: $($user.DisplayName) ($($user.UserPrincipalName))"
Write-Output "Password: $password"
# Export the user details and password to a CSV file
$output = [PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
Password = $password
}
$output | Export-Csv -Path "C:\Users\pathto\userlistpw.csv" -force -NoTypeInformation
}
Error:-
Unable to find type [System.Web.Security.Membership].
At line:13 char:17
+ $password = [System.Web.Security.Membership]::GeneratePassword($p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Web.Security.Membership:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
New-AzureADUser : Cannot bind argument to parameter 'DisplayName' because it is null.
At line:26 char:17
+ New-AzureADUser @newUserParams
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AzureADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.NewUser
User created: ()
Password:
Note:- My csv file contain only Display name , UPN , Block sign in column filled, Else other are blank.
THANK YOU!!
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.
- LainRobertsonSilver Contributor
Strictly speaking, your script is fine.
You are getting two errors from the script:
Unable to find type [System.Web.Security.Membership]
This means the class being referenced - [System.Web.Security.Membership] - does not exist in the machine running the script.
This makes perfect sense if that machine is running a client operation system like Windows 10, as it's not installed by default. Conversely, if you're running a server operating system, it is installed by default.
You can either install the .NET Framework feature on a client system using DISM as shown below or run the script from a server environment.
Checking, installing and then re-checking the ASP.NET feature on a client system
New-AzureADUser : Cannot bind argument to parameter 'DisplayName' because it is null.
This error most likely relates to your CSV file.
Either you have a null value for the DisplayName column for one of your rows or perhaps you have one or more blank rows in your file (i.e. at the end of your file, most likely.)
Fix your CSV file and this will go away.
Cheers,
Lain
- Ajay_Kumar_GhoseBrass ContributorAll seems to be installed and csv file as per the Azure Ad bulk user list.
- you can use the bulk create option in Azure AD directly, refer to the below link for step by step instructions
https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/users-bulk-add- Ajay_Kumar_GhoseBrass ContributorHello 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 youStep 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.