'Extending Bulk AD Accounts Expiration date by 6 months via PowerShell
I am fairly new to PowerShell and this maybe straight forward for a professional.
I am looking to extend expiration date a bulk of AD usernames in a text file by 6 months.
Preferably if the code could pick up the current date and extend from there.
As I have been doing some googling and testing I am come up with the command to do a single account in PowerShell:
Set-ADAccountExpiration SMahmood -DateTime "06/11/2022"
The above command I obviously have to change the username and date (if I run the command on different day) every time I run the command.
I have also managed to find some script of another person who asked a similar question but his script asks you to define the username each time you would like to extend it (this is not my code but has been tested as working) :
$continue = $true
while ($continue) {
write-host " AD Account Expiration Date Changer" -ForegroundColor White
Write-Host ""
while ($true) {
try {
# Loop until a valid username is entered
$Entered_Username_0 = Read-Host "Enter a username"
$Entered_Username = $Entered_Username_0.Trim()
if (Get-ADUser -Identity $Entered_Username | Out-Null) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid username entered!" -ForegroundColor Red
Write-Host ""
}
}
$dateMin = [datetime]::Now
$dateMin_short = $dateMin.ToShortDateString()
Write-Host "Press 1 to extend the account expiration date by 6 months"
Write-Host "Press 2 to extend the account expiration date to a sprecific date"
$Choice_input = Read-Host "Please select an option"
while ($true) {
try {
if ($Choice_input -eq 2) {
while ($true) {
try {
# Loop until a valid Date is entered and that Date is above $dateMin
$Entered_Date = [datetime]::ParseExact(
(Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"),
'dd/MM/yyyy',
[System.Globalization.CultureInfo]::new('en-GB')
)
if ($Entered_Date -lt $dateMin) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid date entered! Format must be DD/MM/YYYY and higher than $dateMin_short." -ForegroundColor Red
Write-Host ""
}
}
}
if ($Choice_input -eq 1) {
$Entered_Date = [datetime]::Now.addmonths(6)
}
else {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Please input a either 1 or 2." -ForegroundColor Red
Write-Host ""
}
}
try {
Set-ADAccountExpiration -Identity $Entered_Username -DateTime $Entered_Date.AddHours(24)
Write-Host ""
Write-Host "New account expiration date for $Entered_Username is $(($Entered_Date).toString('dd/MM/yyyy'))"-ForegroundColor Green
$Entered_Date = ($Entered_date).toString('dd/MM/yyyy')
}
catch {
Write-Host ""
Write-Host "Unable to set account expiry: $_"-ForegroundColor Red
}
Write-Host ""
}
$continue = $true
while ($continue) {
write-host " AD Account Expiration Date Changer" -ForegroundColor White
Write-Host ""
while ($true) {
try {
# Loop until a valid username is entered
$Entered_Username_0 = Read-Host "Enter a username"
$Entered_Username = $Entered_Username_0.Trim()
if (Get-ADUser -Identity $Entered_Username | Out-Null) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid username entered!" -ForegroundColor Red
Write-Host ""
}
}
$dateMin = [datetime]::Now
$dateMin_short = $dateMin.ToShortDateString()
Write-Host "Press 1 to extend the account expiration date by 6 months"
Write-Host "Press 2 to extend the account expiration date to a sprecific date"
$Choice_input = Read-Host "Please select an option"
while ($true) {
try {
if ($Choice_input -eq 2) {
while ($true) {
try {
# Loop until a valid Date is entered and that Date is above $dateMin
$Entered_Date = [datetime]::ParseExact(
(Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"),
'dd/MM/yyyy',
[System.Globalization.CultureInfo]::new('en-GB')
)
if ($Entered_Date -lt $dateMin) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid date entered! Format must be DD/MM/YYYY and higher than $dateMin_short." -ForegroundColor Red
Write-Host ""
}
}
}
if ($Choice_input -eq 1) {
$Entered_Date = [datetime]::Now.addmonths(6)
}
else {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Please input a either 1 or 2." -ForegroundColor Red
Write-Host ""
}
}
try {
Set-ADAccountExpiration -Identity $Entered_Username -DateTime $Entered_Date.AddHours(24)
Write-Host ""
Write-Host "New account expiration date for $Entered_Username is $(($Entered_Date).toString('dd/MM/yyyy'))"-ForegroundColor Green
$Entered_Date = ($Entered_date).toString('dd/MM/yyyy')
}
catch {
Write-Host ""
Write-Host "Unable to set account expiry: $_"-ForegroundColor Red
}
Write-Host ""
}
Big thanks Vihaan Reyansh who provided the script above I had to tweak it a bit as it was changing the description field.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|