initial scripting and csv support

This commit is contained in:
2025-11-03 14:24:59 +01:00
commit 209840bdb1
2 changed files with 89 additions and 0 deletions

View 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."