Set password never expire on bulk via powershell
This code snippet allows you to easily set password never expire for a list of identified users. The required input is a list of unique usernames to be processed. The end result is your users will have the “PasswordNeverExpires” flag set to $false. Meaning the users passwords will expire in line with the applicable password policy.
# Import your list of target users - file contains SamAccountName or DistinguishedName
$colUsers = Get-Content .\TargetUsers.txt
# Loop through our collection of target users
foreach ($objUser in $colUsers) {
# Set an error flag
$ErrorFlag=$false
try{
# Attempt to find a user object for current target user
$ADUserObj = get-aduser $objUser -property PasswordNeverExpires
write-host "Flag was $($ADUserObj.PasswordNeverExpires)"
} Catch {
# On error - set our error flag and output to console
write-host "User not found $objUser"
$ErrorFlag=$true
}
# If no error generated, change the user attribute
if (!$ErrorFlag) {
# Pass the returned user object to the set-aduser cmdlet
$ADUserObj | Set-Aduser -PasswordNeverExpires $false
write-host "Flag cleared for $objUser"
}
}
