AD Last Logon for specific Sub OU

A_Lop 41 Reputation points
2021-03-25T22:41:44.91+00:00

Hi I need assistance on how I can get a list of users in a specific sub OU (sub container) and exported it out. I have this PowerShell script but it's not recognizing the sub ou , it says cannot find an object with the identity "ABCTestgroup" under DC=IT, DA=ITS

Here is the PowerShell script that I used:

Get-ADGroup ABCtestgroup | Get-ADGroupMember | Get-ADUser -Properties Lastlogondate | Select Name, SamAccountName, LastlogonDate | Export-CSV -Path "c:\temp\ADUsers.csv" -NotypeInformation

ABCtextgroup is sub OU of ABCUsers OU . The goal is to find out if the users within the ABCtextgroup are still active users and if not, I want move them in the nonactive users container.

Thanks

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,344 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. A_Lop 41 Reputation points
    2021-03-25T23:14:15.097+00:00

    Any input? Thanks.

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,386 Reputation points Microsoft Vendor
    2021-03-26T04:00:54.42+00:00

    Hi,

    As ABCtestgroup is an OU, not a group, it does not work with Get-ADGroup. You can get the AD users in the OU like this.

    Get-ADUser -filter * -SearchBase "OU=ABCtextgroup ,OU=ABCUsers,DC=IT,DC=ITS" -Properties Lastlogondate | Select Name, SamAccountName, LastlogonDate | Export-CSV -Path "c:\temp\ADUsers.csv"   
    

    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

  3. Qais Mohamed 0 Reputation points
    2023-06-20T10:31:33.1333333+00:00

    Get-ADUser -filter * -SearchBase "OU=ABCtextgroup ,OU=ABCUsers,DC=IT,DC=ITS" -Properties Lastlogondate | Select Name, SamAccountName, LastlogonDate | Export-CSV -Path "c:\temp\ADUsers.csv"

    0 comments No comments