Im a bit of a novice to PowerShell and AD, and am well outside my normal domain (which is web development) but have been asked to do the following. Write a script in Powershell to find ALL users in an Active Directory that have pwdLastSet set to 0, (in a for next loop so I can then process each user).
I came across the code below which will loop through all objects of type People which I assume is what I need to do, but have no idea how to also do the additional check that pwdLastSet value = 0.
Can someone advise?
Thanks in advance
Andy
$Dom = 'LDAP://DC=YourDom;DC=YourExt'
$Root = New-Object DirectoryServices.DirectoryEntry
clear-Host
# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
# Filter the users with -like "CN=Person*". Note the ForEach loop
$adobj= $selector.findall() `
| where {$_.properties.objectcategory -like "CN=Person*"}
ForEach ($person in $adobj)
{
$prop=$person.properties
Write-host "First name: $($prop.givenname) " `
"Surname: $($prop.sn) User: $($prop.cn)"
}
write-host "`nThere are $($adobj.count) users in the $($root.name) domain with password = 0
I came across the code below which will loop through all objects of type People which I assume is what I need to do, but have no idea how to also do the additional check that pwdLastSet value = 0.
Can someone advise?
Thanks in advance
Andy
$Dom = 'LDAP://DC=YourDom;DC=YourExt'
$Root = New-Object DirectoryServices.DirectoryEntry
clear-Host
# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
# Filter the users with -like "CN=Person*". Note the ForEach loop
$adobj= $selector.findall() `
| where {$_.properties.objectcategory -like "CN=Person*"}
ForEach ($person in $adobj)
{
$prop=$person.properties
Write-host "First name: $($prop.givenname) " `
"Surname: $($prop.sn) User: $($prop.cn)"
}
write-host "`nThere are $($adobj.count) users in the $($root.name) domain with password = 0