<#Created By - Abhishek Bansal
Use - Disable Users mentioned in InputUsers.csv and then move them onto a different OU containing all the disabled Users".
Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "InputUsers.csv".
This csv will be containing user details in columns like Samaccountname, DistinguishedName.
#>
Start-Transcript -Path ./Transcript.txt -Append
$disabledOU_DN = Read-Host "Provide DN of Disabled OU = " #***Provide destination OU of disabled Users***#
$userdetails = Import-Csv .\InputUsers.csv
$line = 0 #
$linecount = $userdetails.Count #
$pct = 0 #
foreach($user in $userdetails)
{
$Error.Clear()
$line = $line + 1 #
$pct = $line/$linecount*100 #
Write-Progress -Activity "Checking on $server" -Status "$line out of $linecount" -PercentComplete $pct #
try{
Write-Host -ForegroundColor Cyan "`nDisabling User account $($User.SamAccountName)"
Disable-ADAccount -Identity $user.SamAccountName-PassThru -ErrorAction Stop | Export-Csv ./DisableUsersResult.csv -NoTypeInformation -Append
Write-Host -ForegroundColor Green "Succesfully disable $($User.SamAccountName) ID."
Write-Host -ForegroundColor Cyan "`nMoving User account $($User.SamAccountName) to $disabledOU_DN now.."
Move-ADObject -Identity $user.DistinguishedName -TargetPath $disabledOU_DN -PassThru -ErrorAction Stop | Export-Csv ./MoveUserResult.csv -NoTypeInformation -Append
Write-Host -ForegroundColor Green "Succesfully moved $($User.SamAccountName) ID."
}
catch{
Write-Host -ForegroundColor Red "Error handling $($User.SamAccountName) ID."
$user | Select-Object -Property @{n="User ID";e={$user.SamAccountName}},@{n="Error";e={$Error.exception.message}} | Export-Csv ./DisableUsersErrorlogs.csv -NoTypeInformation -Append
}
}
Stop-Transcript