troubleshooting Question

PowerShell script to Expire all Passwords

Avatar of SysAdmin BBS
SysAdmin BBSFlag for United States of America asked on
Active DirectoryShell Scripting
13 Comments2 Solutions1951 ViewsLast Modified:
I have been tasked with writing a Powershell script to Expire all passwords in the domain as a test.  I have a test environment to use so don't worry.  I haven't been able to get it to run successfully and could use some help.

I have probably botched this script pretty bad, so be kind.  I'm trying to learn scripting for the first time ever.

This script changes the "Password Expired" value (PasswordExpired) to True 

$PEValue = $true
#$true to Expire the user password
#You cannot un-Expire a password after expiring it
$ObjFilter = "(&(objectCategory=person)(objectCategory=User))" 
    $objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Users,OU=TestEnvironment,DC=mydomain,DC=com")
    $objSearch = New-Object System.DirectoryServices.DirectorySearcher 
    $objSearch.PageSize = 15000 
    $objSearch.Filter = $ObjFilter  
    $objSearch.SearchRoot = $objOU
    $objSearch.SearchScope = "Subtree" 
    $AllObj = $objSearch.FindAll() 
    foreach ($Obj in $AllObj) 
            $objItemS = $Obj.Properties
            $UserN = $
            $UserDN = $objItemS.distinguishedname
            $user = [ADSI] "LDAP://$userDN"
            Write-host -NoNewLine "Modifying $UserN Properties...."
            Write-host "Password has Expired!"
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros