locked
Search for new and disabled accounts RRS feed

  • Question

  • Hi everyone,

    How do I use PS to find all new accounts created between 1 June 2012 and 3 October 2012 in AD?  Also how do I use PS to find all accounts that got disabled between 1 June 2012 and 3 October 2012?  Thanks so much in advance.

    • Moved by Yan Li_ Friday, October 5, 2012 5:45 AM (From:Directory Services)
    Thursday, October 4, 2012 2:59 PM

Answers

  • If you are looking for assistance with scripting, you might want to refer to the PowerShell forum http://social.technet.microsoft.com/Forums/en/winserverpowershell/threads
    http://social.technet.microsoft.com/Forums/en-US/category/scripting

    In addition see this too.
    Using PowerShell to Find All Disabled Users in Active Directory
    http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/16/using-powershell-to-find-all-disabled-users-in-active-directory.aspx


    Best Regards,

    Sandesh Dubey.

    MCSE|MCSA:Messaging|MCTS|MCITP:Enterprise Adminitrator | My Blog

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    • Proposed as answer by Abhijit Waikar Friday, October 5, 2012 3:37 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Thursday, October 4, 2012 3:07 PM
  • Hi,

    Additionally see this:
    Using PowerShell to Find All Disabled Users in Active Directory
    http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/16/using-powershell-to-find-all-disabled-users-in-active-directory.aspx

    powershell finding new user accounts
    http://synjunkie.blogspot.in/2008/11/powershell-finding-new-user-accounts.html

     


    Best regards,

    Abhijit Waikar.
    MCSA | MCSA:Messaging | MCITP:SA | MCC:2012
    Blog: http://abhijitw.wordpress.com
    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees and confers no rights.

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 3:43 AM
  • Please use the below script to get the newly created accounts. Please modify the necessary parameters accordingly.

    ##########################################################

    param ($OUDNPath= "OU=XXXL,OU=yyy,DC=zzz,DC=COM", $recentusersoutputfile ="D:\scripts\recentusers.csv")

    $importusers = $null
    $objusers  = $null
    $a= Get-Date
    $createdsince=$a.AddDays(-2)

    function Export-Recentusers
     {
                   $objuser= $null
      [array]$exportlist= $null
      $objou= [ADSI]"LDAP://$OUDNPATH"
                    $searchad= [System.DirectoryServices.DirectorySearcher]$objou
                    $searchad.Filter ="(&(objectcategory=person)(objectclass=user))"
                    $searchad.PageSize= 1000
                    $searchad.searchscope= "Subtree"
                    [void]$searchad.PropertiesToLoad.Add("distinguishedname")
                    [void]$searchad.PropertiesToLoad.Add("samaccountname")
                    [void]$searchad.PropertiesToLoad.Add("whencreated")
      [void]$searchad.PropertiesToLoad.Add("cn")
            [void]$searchad.PropertiesToLoad.Add("mail")

      $objusers = $searchad.findall()
      foreach ($evuser in $objusers )
       {
       if ($($evuser.properties.whencreated) -ge $createdsince)
        {
        $createdate= $($evuser.properties.whencreated)
        $createdate= $createdate.tolocaltime()
     $objps= new-object psobject -property @{DN= $($evuser.properties.distinguishedname);samaccountname=$($evuser.properties.samaccountname);cn=$($evuser.properties.cn);whencreated=$createdate;Email=$($evuser.properties.mail) }
     $exportlist += $objps

        
        }
       }
      if ($exportlist)
      {

      $exportlist | select DN,samaccountname,cn,whencreated,Email | export-csv $recentusersoutputfile -notype
      }
     }
    Export-Recentusers

     

      $recipients1 = "xxx.email.com"

      $body1 = "Hi All"
      $body2 = "Please find attached sheet with user account created since $createdsince"
      $body3 = "Initiate prepare move for users who is having a valid email address (as per the sheet), and keep a track of users without a vaild email address."

      send-mailmessage -from xyz.mail.com -to $recipients1 -subject "User Accounts created after $createdsince" -body ($body1,$body2,$body3) -smtpServer 10.10.1.150 -BodyAsHtml -Attachments $recentusersoutputfile

    ###############################################################################################


    Sajeed AM|+919846553328

    • Proposed as answer by Bigteddy Friday, October 5, 2012 6:10 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 4:36 AM
  • Hi,

    The below script will find new created users for the last month:

      $createdate = (Get-Date).AddDays(-30) 
      Get-ADUser -Filter * -Properties * | ` 
        where { $_.whenCreated -ge $createdate } | select Name,whenCreated ` 
        | Export-Csv -Path c:\ps\NewAdusers.csv" 
     

    For more details:

    http://social.technet.microsoft.com/wiki/contents/articles/7604.how-to-get-a-list-of-user-accounts-created-on-a-given-day-or-over-a-given-period.aspx

    http://gallery.technet.microsoft.com/scriptcenter/Find-Active-Directory-26b71b73

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    • Proposed as answer by Bigteddy Friday, October 5, 2012 6:30 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 6:15 AM
  • My additions: for large domain it is more efficient use Filter:

    $createdate = (Get-Date).AddDays(-30) 
    Get-ADUser -Filter  'whenCreated -ge $createdate'  | select Name,whenCreated  | Export-Csv -Path c:\ps\NewAdusers.csv" 
    


    Сазонов Илья http://isazonov.wordpress.com/

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 8:17 AM
  • LOL. I was going to ask if the OP was asking the same question a second time because he was unsatisfied with the response the first time: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/462f3f56-260a-449e-9d96-1303871b1676

    But then I noticed he got more detailed responses here. Still, nobody here seems to disagree with what we told him in the other thread, namely that AD does not keep a record of *when* an account was disabled, so I guess we were right about that part. ;-)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 4:13 PM

All replies

  • If you are looking for assistance with scripting, you might want to refer to the PowerShell forum http://social.technet.microsoft.com/Forums/en/winserverpowershell/threads
    http://social.technet.microsoft.com/Forums/en-US/category/scripting

    In addition see this too.
    Using PowerShell to Find All Disabled Users in Active Directory
    http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/16/using-powershell-to-find-all-disabled-users-in-active-directory.aspx


    Best Regards,

    Sandesh Dubey.

    MCSE|MCSA:Messaging|MCTS|MCITP:Enterprise Adminitrator | My Blog

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    • Proposed as answer by Abhijit Waikar Friday, October 5, 2012 3:37 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Thursday, October 4, 2012 3:07 PM
  • Hi,

    Additionally see this:
    Using PowerShell to Find All Disabled Users in Active Directory
    http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/16/using-powershell-to-find-all-disabled-users-in-active-directory.aspx

    powershell finding new user accounts
    http://synjunkie.blogspot.in/2008/11/powershell-finding-new-user-accounts.html

     


    Best regards,

    Abhijit Waikar.
    MCSA | MCSA:Messaging | MCITP:SA | MCC:2012
    Blog: http://abhijitw.wordpress.com
    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees and confers no rights.

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 3:43 AM
  • Please use the below script to get the newly created accounts. Please modify the necessary parameters accordingly.

    ##########################################################

    param ($OUDNPath= "OU=XXXL,OU=yyy,DC=zzz,DC=COM", $recentusersoutputfile ="D:\scripts\recentusers.csv")

    $importusers = $null
    $objusers  = $null
    $a= Get-Date
    $createdsince=$a.AddDays(-2)

    function Export-Recentusers
     {
                   $objuser= $null
      [array]$exportlist= $null
      $objou= [ADSI]"LDAP://$OUDNPATH"
                    $searchad= [System.DirectoryServices.DirectorySearcher]$objou
                    $searchad.Filter ="(&(objectcategory=person)(objectclass=user))"
                    $searchad.PageSize= 1000
                    $searchad.searchscope= "Subtree"
                    [void]$searchad.PropertiesToLoad.Add("distinguishedname")
                    [void]$searchad.PropertiesToLoad.Add("samaccountname")
                    [void]$searchad.PropertiesToLoad.Add("whencreated")
      [void]$searchad.PropertiesToLoad.Add("cn")
            [void]$searchad.PropertiesToLoad.Add("mail")

      $objusers = $searchad.findall()
      foreach ($evuser in $objusers )
       {
       if ($($evuser.properties.whencreated) -ge $createdsince)
        {
        $createdate= $($evuser.properties.whencreated)
        $createdate= $createdate.tolocaltime()
     $objps= new-object psobject -property @{DN= $($evuser.properties.distinguishedname);samaccountname=$($evuser.properties.samaccountname);cn=$($evuser.properties.cn);whencreated=$createdate;Email=$($evuser.properties.mail) }
     $exportlist += $objps

        
        }
       }
      if ($exportlist)
      {

      $exportlist | select DN,samaccountname,cn,whencreated,Email | export-csv $recentusersoutputfile -notype
      }
     }
    Export-Recentusers

     

      $recipients1 = "xxx.email.com"

      $body1 = "Hi All"
      $body2 = "Please find attached sheet with user account created since $createdsince"
      $body3 = "Initiate prepare move for users who is having a valid email address (as per the sheet), and keep a track of users without a vaild email address."

      send-mailmessage -from xyz.mail.com -to $recipients1 -subject "User Accounts created after $createdsince" -body ($body1,$body2,$body3) -smtpServer 10.10.1.150 -BodyAsHtml -Attachments $recentusersoutputfile

    ###############################################################################################


    Sajeed AM|+919846553328

    • Proposed as answer by Bigteddy Friday, October 5, 2012 6:10 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 4:36 AM
  • Hi,

    The below script will find new created users for the last month:

      $createdate = (Get-Date).AddDays(-30) 
      Get-ADUser -Filter * -Properties * | ` 
        where { $_.whenCreated -ge $createdate } | select Name,whenCreated ` 
        | Export-Csv -Path c:\ps\NewAdusers.csv" 
     

    For more details:

    http://social.technet.microsoft.com/wiki/contents/articles/7604.how-to-get-a-list-of-user-accounts-created-on-a-given-day-or-over-a-given-period.aspx

    http://gallery.technet.microsoft.com/scriptcenter/Find-Active-Directory-26b71b73

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    • Proposed as answer by Bigteddy Friday, October 5, 2012 6:30 AM
    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 6:15 AM
  • My additions: for large domain it is more efficient use Filter:

    $createdate = (Get-Date).AddDays(-30) 
    Get-ADUser -Filter  'whenCreated -ge $createdate'  | select Name,whenCreated  | Export-Csv -Path c:\ps\NewAdusers.csv" 
    


    Сазонов Илья http://isazonov.wordpress.com/

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 8:17 AM
  • LOL. I was going to ask if the OP was asking the same question a second time because he was unsatisfied with the response the first time: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/462f3f56-260a-449e-9d96-1303871b1676

    But then I noticed he got more detailed responses here. Still, nobody here seems to disagree with what we told him in the other thread, namely that AD does not keep a record of *when* an account was disabled, so I guess we were right about that part. ;-)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by Yan Li_ Monday, October 15, 2012 2:41 AM
    Friday, October 5, 2012 4:13 PM