locked
Multiple OUs for finding Users that have not logged in for 90 days RRS feed

  • Question

  • So I created this script but it is not honoring the OUs.  It is searching the entire domain, any help is appreciated!

    $OUs = "OU1", "OU2"
    $d = (get-date).adddays(-90)
    
    $users = $OUs | Foreach {
      Get-AdUser -filter {LastLogonTimeStamp -lt $d -and enabled -eq $true} -Property lastlogondate | sort-object -property lastlogondate -descending | select name, lastlogondate  | 
        select-object name, SAMaccountname, passwordExpired, PasswordNeverExpires, logoncount, whenCreated, lastlogondate, PasswordLastSet, lastlogontimestamp 
    }
    
    $users | Export-Csv -LiteralPath "c:\temp\No Login Past 90 Days.csv" -NoTypeInformation
    $users

    Thursday, July 19, 2018 1:17 AM

All replies

  • If you would take the time to learn basic PowerShell and learn how to use help you would be able to solve these simple issues on your own.

    $OUs = "OU1", "OU2"
    $d = (get-date).adddays(-90)
    $properties = @(
        'name', 
        'SAMaccountname', 
        'passwordExpired', 
        'PasswordNeverExpires', 
        'logoncount', 
        'whenCreated', 
        'lastlogondate', 
        'PasswordLastSet', 
        'lastlogontimestamp'
    )
    
    $users = $OUs | 
        ForEach-Object {
            Get-AdUser -filter {LastLogonTimeStamp -lt $d -and enabled -eq $true} -Properties $properties 
        } |
        sort-object -property lastlogondate -descending |
        select-object $properties
    
    $users | Export-Csv 'c:\temp\No Login Past 90 Days.csv' -NoTypeInformation
    $users

    Guessing and asking other to do your learning and thinking will not help you become a technician.

    You have some many amateur mistakes that show you have not even attempted to read the basic documentation.  A technician lives in the documentation of the system and its subsystems.  You cannot be a technician if you cannot or will not do this.


    \_(ツ)_/


    • Edited by jrv Thursday, July 19, 2018 2:45 AM
    Thursday, July 19, 2018 2:42 AM
  • Look at EXAMPLE 1 and read about the SearchBase parameter in the documentation for the Get-ADUser cmdlet.
    Thursday, July 19, 2018 7:17 AM