Create User Accounts in Bulk via powershell

Configurare noua (How To)

Situatie

Here’s a PowerShell script to create user accounts in bulk using data from a CSV file:

Backup

# Path to the CSV file containing user details (e.g., Name, SamAccountName, UserPrincipalName, Password)
$csvFilePath = “C:\Path\to\users.csv”

# Import user details from the CSV file
$users = Import-Csv $csvFilePath

# Loop through each user and create their account
foreach ($user in $users) {
$name = $user.Name
$samAccountName = $user.SamAccountName
$userPrincipalName = $user.UserPrincipalName
$password = $user.Password

# Check if the user account already exists
if (-not (Get-ADUser -Filter {SamAccountName -eq $samAccountName})) {
# Create the new user account
New-ADUser -Name $name `
-SamAccountName $samAccountName `
-UserPrincipalName $userPrincipalName `
-AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) `
-Enabled $true
Write-Host “User $samAccountName created successfully”
}
else {
Write-Host “User $samAccountName already exists”
}
}

Before running the script, ensure you have a CSV file (e.g., users.csv) containing at least four columns: Name, SamAccountName, UserPrincipalName, and Password. Each row in the CSV file should contain the name, SamAccountName, UserPrincipalName, and password for a user to be created.

Example CSV file (users.csv):

Name,SamAccountName,UserPrincipalName,Password
John Doe,johndoe,johndoe@example.com,Pa$$w0rd
Jane Smith,janesmith,janesmith@example.com,Pa$$w0rd

Replace “C:\Path\to\users.csv” with the actual path to your CSV file.

This script will read user details from the CSV file, loop through each user, check if the user account already exists in Active Directory, and create a new user account if it doesn’t exist. If the user account already exists, it will display a message indicating that the user already exists.

Solutie

Tip solutie

Permanent

Voteaza

(4 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?