Repository navigation
Script
Pre-release
Pre-release
# Script Information
# This script automates common Active Directory tasks like user creation, password resets, and group management.
# It includes logging and customizable parameters for user flexibility.
# Tested using PowerShell 5.1
# Additionally, make sure that the Active Directory module is installed, which typically comes with the RSAT (Remote Server Administration Tools) package on client operating systems or is natively available on domain controllers.
# If missing please run the following PS command:
# Get-WindowsCapability -Name RSAT* -Online | Add-WindowsCapability -Online
# OR
# Install-WindowsFeature -Name "RSAT-AD-Tools" -IncludeAllSubFeature
# Ensure the Active Directory module is imported
Import-Module ActiveDirectory
# Define log file path
$logFilePath = "C:\AD_Automation\ADAutomation.log"
# Function to log messages
function Write-Log {
param (
[string]$logMessage
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp : $logMessage"
Add-Content -Path $logFilePath -Value $logEntry
}
# Create log directory if it doesn't exist
if (-not (Test-Path "C:\AD_Automation")) {
New-Item -Path "C:\AD_Automation" -ItemType Directory
}
# Log script start
Write-Log "Script started."
# Function to create a new user
function Create-ADUser {
param (
[string]$UserName,
[string]$GivenName,
[string]$Surname,
[string]$Password,
[string]$OU
)
try {
New-ADUser -Name $UserName -GivenName $GivenName -Surname $Surname `
-UserPrincipalName "$UserName@yourdomain.com" -SamAccountName $UserName `
-Path $OU -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) `
-Enabled $true -PasswordNeverExpires $false -ChangePasswordAtLogon $true
Write-Log "User '$UserName' created successfully."
} catch {
Write-Log "Failed to create user '$UserName': $_"
}
}
# Function to reset a user's password
function Reset-ADUserPassword {
param (
[string]$UserName,
[string]$NewPassword
)
try {
Set-ADAccountPassword -Identity $UserName -NewPassword (ConvertTo-SecureString $NewPassword -AsPlainText -Force) -Reset
Unlock-ADAccount -Identity $UserName
Write-Log "Password reset successfully for user '$UserName'."
} catch {
Write-Log "Failed to reset password for user '$UserName': $_"
}
}
# Function to add a user to a group
function Add-ADUserToGroup {
param (
[string]$UserName,
[string]$GroupName
)
try {
Add-ADGroupMember -Identity $GroupName -Members $UserName
Write-Log "User '$UserName' added to group '$GroupName'."
} catch {
Write-Log "Failed to add user '$UserName' to group '$GroupName': $_"
}
}
# Example usage (customize as needed)
$OU = "OU=Users,DC=yourdomain,DC=com"
# Create a new user
Create-ADUser -UserName "jdoe" -GivenName "John" -Surname "Doe" -Password "P@ssw0rd123" -OU $OU
# Reset a user's password
Reset-ADUserPassword -UserName "jdoe" -NewPassword "N3wP@ssw0rd!"
# Add the user to a group
Add-ADUserToGroup -UserName "jdoe" -GroupName "IT-Support"
# Log script end
Write-Log "Script finished."
# End of script
Full Changelog: https://github.com/evyatarwi/AD-User-Management-Script/commits/Powershell