-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUserAD.ps1
More file actions
66 lines (55 loc) · 2.49 KB
/
CreateUserAD.ps1
File metadata and controls
66 lines (55 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
.SYNOPSIS
This script creates Active Directory users from a list in a text file.
.DESCRIPTION
Reads usernames from a text file, each on a new line, and creates Active Directory users with initial passwords set to "Buffalo" followed by the current year. The password is set to expire according to the domain policy. Fields have been set to REDACTED for security purposes and can be edited. UserParams can be added as needed as this provides a skeleton
.NOTES
Version: 1.0
Author: strangeprogram
Creation Date: 04/14/21
Purpose/Change: Initial script development was made to automate user creation for your organization.
.EXAMPLE
.\CreateADUsers.ps1
#>
# Import Active Directory Module
Import-Module ActiveDirectory
# Path to the text file containing the usernames
$textFilePath = "C:\path\to\your\textfile.txt"
# Read each line as a username from the text file
$usernames = Get-Content $textFilePath
# The current year
$currentYear = (Get-Date).Year
# The password to be set for each user
$password = "Buffalo" + $currentYear
# Convert the password to a SecureString
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
# Loop through each username and create the user in Active Directory
foreach ($username in $usernames) {
# Check if user already exists
$userExists = Get-ADUser -Filter "SamAccountName -eq '$username'" -Server "REDACTED" -ErrorAction SilentlyContinue
if ($userExists) {
Write-Output "User $username already exists in AD."
} else {
# User attributes - adjust as necessary
$userParams = @{
SamAccountName = $username
UserPrincipalName = "$username@REDACTED"
Name = $username
GivenName = $username
Surname = $username
Enabled = $true
AccountPassword = $securePassword
ChangePasswordAtLogon = $true # Enforce the password change at next logon
PasswordNeverExpires = $false # Password will expire according to domain policy
Path = "OU=Users,DC=REDACTED,DC=com" # Specify the correct OU path
Server = "REDACTED" # Specify the domain controller
}
# Create the new user
try {
New-ADUser @userParams
Write-Output "User $username created successfully with a password that will expire according to domain policy."
} catch {
Write-Error "An error occurred creating user $username: $_"
}
}
}