2

This is the PowerShell script I use to find the LogOn attribute from users in Active Directory. Now rather than getting all users I want it to search only in a specific OU. The domain looks like this: Domain - OU (users and groups) - OU (Users, OU inside the Users and groups OU)

$as = [adsisearcher]"(&(objectClass=person)(objectCategory=user))" 
$as.PropertiesToLoad.Add('cn')
$as.PropertiesToLoad.Add('lastlogon')
$as.PageSize = 200
$as.FindAll() | ForEach-Object {
    $props = @{ 'CN' = ($_.properties.item('cn') | Out-String).Trim()
                'LastLogon' = ([datetime]::FromFiletime(($_.properties.item('lastlogon') | Out-String).Trim())) }
    New-Object psObject -Property $props
    } | Export-Csv 'Lastlogon.csv' -NoTypeInformation
1

Assign a value to the SearchRoot property of the searcher object.

$as.SearchRoot = "LDAP://dc=fabrikam, dc=com"

More at https://technet.microsoft.com/en-us/library/ff730967.aspx

0

I find LogOn information from a specific OU via Powershell using the following

Get-ADUser -Filter * -SearchBase "OU=SpecificOU,OU=ContosoUsers,DC=Contoso,DC=local" -Properties * | Select-Object -Property Name, lastlogondate | Export-Csv C:\lastlogon.csv

The above "one-liner" will produce the Name and LastLogonDate for all of the user accounts in the SpecificOU under the ContosoUsers OU in the Active Directory Domain Contoso.local and then export the contents into a .CSV file at the root of C:.

-SearchBase is the key in the one-liner above. With it, you can specify a specific Active Directory path to search under.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.