List of users from a specific OU in the Activer Directory

I want to a list of users that is in the following OU:

Example.domain.se

… OU Systemaccounts <= here

…OU Active Directory

…OU Administrators <= here

The attributes I want to list for each user are:
-SamAccountName
-AccountType
-enabledAccount
-ExpirationDate
-LockedOut
-DisplayName
-LastLogonDat
-and which ou the user are listed in

Really appreciate your answer and all the guiding advice I can get.

What have you tried so far and what did not work as expected? Please share your code.

Please keep in mind: this forum is more about “to get help with your code” than “to get ready to use code on request:wink:

In general: To query the AD for user we usually use

Please read the help completely including the examples to learn how to use it.

$path = "OU=Administrators,OU=ActiveDirectory,OU=SystemAccounts,DC=test,DC=domain,DC=com"
$users = get-aduser -SearchBase $path -filter -properties SamAccountName, enabled, AccountExpirationDate, LockedOut,DisplayName,LastLogontimestamp | Out-File -FilePath .\test.txt

But then I have to display only active accounts, not disabled accounts or in a another word not get “disabled users” added to my test.txt file.

Please, when you post code format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance.

If you just want enabled accounts you should filter for enabled accounts :wink:

$SearchBase = 'OU=Administrators,OU=ActiveDirectory,OU=SystemAccounts,DC=test,DC=domain,DC=com'
Get-ADUser -SearchBase $SearchBase -Filter "Enabled -eq '$true'" -Properties AccountExpirationDate, LockedOut,DisplayName,LastLogontimestamp |
    Select-Object -Property SamAccountName, enabled, AccountExpirationDate, LockedOut,DisplayName,LastLogontimestamp |
        Export-Csv -Path .\test.csv -NoTypeInformation -Delimiter ','

I’d recommend using a CSV file instead of a plain text file.

1 Like

Thanks for your advice, now I have to do some formating, so non-it-user kan read the csv-file too

Depending on your language setting you can set another delimiter for your CSV file so Excel would recognise it properly. Another option would be to use the great module from Doug Finke ImportExcel.

1 Like

Thanks a lot.
Only one issue left, I also want to display only those user that´s have a certain description
As en example “descrition company123”
I tried a lot of combinations but without any success.
The starting code looks like this:

$SearchBase = 'OU=Administrators, OU=ActiveDirectory,OU=systemaccounts,DC=test,DC=domain,DC=com'
$users = Get-ADUser -SearchBase $SearchBase -Filter "Enabled -eq '$true'"-Properties AccountExpirationDate, LockedOut,DisplayName,LastLogon,Description  |
    Select-Object -Property SamAccountName, AccountExpirationDate, LockedOut,DisplayName,LastLogon,Description

But I guess the code needs to be completed with something logical like this:

description -eq company123

If you cannot filter at the source of the information you always can use a

$SearchBase = 'OU=Administrators, OU=ActiveDirectory,OU=systemaccounts,DC=test,DC=domain,DC=com'
Get-ADUser -SearchBase $SearchBase -Filter "Enabled -eq '$true'"-Properties AccountExpirationDate, LockedOut, DisplayName, LastLogon,Description  |
    Where-Object -Property Description -Match 'company123' |
        Select-Object -Property SamAccountName, AccountExpirationDate, LockedOut, DisplayName, LastLogon, Description |
            Export-Csv -Path .\test.csv -NoTypeInformation -Delimiter ','
1 Like