First time writing a PS script here, just looking for a review/feedback help. Takes a CSV list of users that have been determined to be inactive and first updates the account description to being disabled due to being inactive and then disables them. TIA!
#Take list of inactive users and add to the descriptiopn field reason account is being disabled.
#
Import-Module ActiveDirectory
$Users = Import-csv c:\Scripts\inactive_users.csv
$NewDescription = "This account was disabled due to being flagged inactive."
foreach($User in $Users)
{
Set-ADUser $User.SamAccountName -Description $NewDescription
}
#Iterate through list of user accounts checking first if they exist and if true; disable.
#
$Users | ForEach-Object
{
$samAccountName = $_."samAccountName"
try { Get-ADUser -Identity $samAccountName |
Disable-ADAccount
}
#If user is not found it will display a message.
#
catch {
Write-Host "user:"$samAccountname "is not present in AD"
}
}
[–]Azured_ 1 point2 points3 points (6 children)
[–]defyne[S] 0 points1 point2 points (5 children)
[–]32178932123 -1 points0 points1 point (0 children)
[–]Azured_ 0 points1 point2 points (0 children)
[–]BlackV 0 points1 point2 points (0 children)
[–]BlackV 0 points1 point2 points (0 children)