initial scripting and csv support
This commit is contained in:
57
user adding AD/user-add.ps1
Normal file
57
user adding AD/user-add.ps1
Normal file
@@ -0,0 +1,57 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds new Active Directory users from a CSV file.
|
||||
.DESCRIPTION
|
||||
Reads a CSV with user details and creates corresponding AD accounts.
|
||||
.NOTES
|
||||
Requires RSAT / ActiveDirectory module.
|
||||
#>
|
||||
Import-Module ActiveDirectory
|
||||
|
||||
# Path to CSV file
|
||||
$csvPath = ".\users.csv"
|
||||
|
||||
# Import from CSV
|
||||
$users = Import-Csv -Path $csvPath
|
||||
|
||||
foreach ($user in $users) {
|
||||
$FirstName = $user.FirstName
|
||||
$LastName = $user.LastName
|
||||
$Username = $user.Username
|
||||
$OU = $user.OU
|
||||
$Password = (ConvertTo-SecureString $user.Password -AsPlainText -Force)
|
||||
$Department = $user.Department
|
||||
$Title = $user.Title
|
||||
$DisplayName = "$FirstName $LastName"
|
||||
$Email = "$Username@example.com"
|
||||
|
||||
# Check if user already exists
|
||||
if (Get-ADUser -Filter {SamAccountName -eq $Username}) {
|
||||
Write-Host "User $Username already exists, skipping..." -ForegroundColor Yellow
|
||||
continue
|
||||
}
|
||||
|
||||
# Create the user
|
||||
try {
|
||||
New-ADUser `
|
||||
-SamAccountName $Username `
|
||||
-UserPrincipalName $Email `
|
||||
-Name $DisplayName `
|
||||
-GivenName $FirstName `
|
||||
-Surname $LastName `
|
||||
-DisplayName $DisplayName `
|
||||
-Path $OU `
|
||||
-Department $Department `
|
||||
-Title $Title `
|
||||
-AccountPassword $Password `
|
||||
-Enabled $true `
|
||||
-ChangePasswordAtLogon $true
|
||||
|
||||
Write-Host "Created user: $DisplayName ($Username)" -ForegroundColor Green
|
||||
}
|
||||
catch {
|
||||
Write-Host "Failed to create user $Username: $_" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "User import complete."
|
||||
Reference in New Issue
Block a user