List Users Last Logon in specific Sub OU

Arni 116 Reputation points

Hello I need assistance in finding out users' last logon on a specific sub ou. I used this PS command that I got here in the forum, but this only provides the parent ou not the child ou:

Get-ADUser -filter * -SearchBase 'ou=Offices,DC=contoso,DC=com' -Properties LastLogonTimeStamp | Select sAmAccountName,@{label="LastLogonTimeStamp";Expression={if($.LastLogonTimeStamp){[datetime]::FromFileTime([Int64]$.LastLogonTimeStamp)} Else {"Never"}}}

I need the sub ou members. I would really appreciate if someone could assist. Thank you.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
4,008 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,766 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 36,331 Reputation points

    Try adding -SearchScope Subtree to the Get-ADUser cmdlet.

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 18,846 Reputation points Microsoft Vendor


    You can simply specify the sub OU

    Get-ADUser -filter * -SearchBase 'OU=subou,OU=Offices,DC=contoso,DC=com' -Properties LastLogonTimeStamp  

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.