List Users Last Logon in specific Sub OU

Arni 116 Reputation points
2021-03-26T16:18:00.693+00:00

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.
5,899 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.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2021-03-29T02:25:57.567+00:00

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

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,971 Reputation points Microsoft Vendor
    2021-03-29T08:46:49.643+00:00

    Hi,

    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.

    0 comments No comments