Creating a List of Dummy Users in Active Directory using PowerShell

Ilya Fedotov

12/21/20232 min read

woman jumping near white wall paint
woman jumping near white wall paint

PowerShell Script for Active Directory User Creation with CSV and Log Output

This PowerShell script is designed to automate the process of creating a specified number of user accounts in Active Directory. It generates a unique username and a complex password for each user. Key features of this script include:

  1. User and Password Generation: Creates a predefined number of users (default is 10) with complex passwords, adhering to standard complexity requirements.

  2. Active Directory Integration: Utilizes the Active Directory module to create each user account in a specified Organizational Unit (OU).

  3. CSV Output: For each created user, the script appends the username and password to a CSV file. This file serves as a record of all created accounts and their associated passwords.

  4. Detailed Logging: The script maintains a log file, recording the success or failure of each user account creation. This log provides a detailed account of the script's operations and any errors encountered.

  5. Customization and Security: Parameters such as the number of users, OU path, and file paths for the CSV and log files can be customized. The script emphasizes the secure handling of sensitive information like passwords.

This tool is particularly useful for administrators needing to quickly set up multiple user accounts in Active Directory with strong passwords, while keeping a record of the created accounts and monitoring the process through detailed logs.

Notes:

  • File Paths: Change $csvFilePath and $logFilePath to the desired locations on your system.

  • Security Considerations: Storing passwords in a CSV file can be a security risk. Ensure that this file is stored securely and consider encrypting it if necessary.

  • Error Handling: The script writes both successful creations and errors to the log file.

  • Execution Environment: Make sure the path where you're writing the files has the appropriate write permissions.

  • Testing: As always, test this script in a safe environment before using it in a production setting.

This script will create a CSV file with a list of usernames and their passwords, and a log file detailing the user creation process. Remember to handle and store these files securely, as they contain sensitive information.

param (

[int]$NumberOfUsers = 10 # Default number of users to create

)

function Generate-ComplexPassword {

$passwordLength = 12

$passwordChars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789!@$?_"

$random = new-object System.Random

$newPassword = for($i=0; $i -lt $passwordLength; $i++) {

$passwordChars[$random.Next(0,$passwordChars.Length)]

}

return -join $newPassword

}

Import-Module ActiveDirectory

# File paths for CSV and log

$csvFilePath = "C:\Path\To\Your\Output\users.csv"

$logFilePath = "C:\Path\To\Your\Output\log.txt"

# Header for CSV file

"username,password" | Out-File -FilePath $csvFilePath -Encoding UTF8

for ($i = 1; $i -le $NumberOfUsers; $i++) {

$username = "User$i"

$password = Generate-ComplexPassword

$userDetails = @{

SamAccountName = $username

UserPrincipalName = "$username@yourdomain.com"

Name = $username

Enabled = $true

ChangePasswordAtLogon = $false

AccountPassword = (ConvertTo-SecureString -AsPlainText $password -Force)

Path = "OU=Users,DC=yourdomain,DC=com" # Change this to your desired OU

}

try {

New-ADUser @userDetails

"$username,$password" | Out-File -FilePath $csvFilePath -Append -Encoding UTF8

"Successfully created user: $username" | Out-File -FilePath $logFilePath -Append -Encoding UTF8

} catch {

"Error creating user: $username. Error: $_" | Out-File -FilePath $logFilePath -Append -Encoding UTF8

}

}