Link to home
Start Free TrialLog in
Avatar of youpa
youpa

asked on

Export Active Directory Users Attribute of all users in a specify location

Hello,

Anyone know a tools where I can set in a windows schedule  task that will export in a CSV the First name & Last Name & Email Address & Display name of all the user located in a specific OU.

I have a Domain Control running Windows Server 2008

thanks
Avatar of Mike Kline
Mike Kline
Flag of United States of America image

You can use powershell and get-aduser  also lookup adinfo (free gui tool)

Thanks

Miks
change the names as per your naming convention and configure below command in scheduled task

dsquery user "OU,OUname,DC=domain,DC=local" -limit 0 | dsget user -display -email -pager>c:\Users.csv

Open in new window

Command below will export all attributes for users:

Dsquery * -limit 0 -filter "&(objectClass=User)(objectCategory=Person)" -attr * >> output.txt
Avatar of w_richard
w_richard

For exporting this can be done manually...

This scripts is tested scripts...

Hope it helps you.

Import-Module ActiveDirectory

#Create a variable for the date stamp in the log file
$LogDate = get-date -f yyyyMMddhhmm
#Sets the OU to do the base search for all user accounts, change for your env.
$SearchBase = "OU=****,DC=****,DC=com"
#Create an empty array for the log file
$LogArray = @()

$Users = @(Import-Csv "e:\temp\Leavers.csv")
ForEach ($User In $Users)
{
$Filter = "(company=" + $User."Employee ID" + ")"
$Return = Get-ADUser -searchbase $SearchBase -LDAPFilter $Filter
#Create new object for logging
If ($Return){
#Amend the users description
set-aduser $Return -Description ("Identified Leaver - Disabled on "+(get-date).toshortdatestring())
#Disables user object.
Disable-ADAccount $Return
#Moves the account
Move-ADObject $Return -TargetPath "OU=Users,OU=****,OU=Inactifs,DC=****,DC=com"
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Return.name
$obj | Add-Member -MemberType NoteProperty -Name "Account Name" -Value $Return.samaccountname
$obj | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $User."Employee ID"
$obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $Return.DistinguishedName
#Adds object to the log array
$LogArray += $obj
}
}

#Exports log array to HTML file in the temp directory with a date and time stamp in the file name.
$a = "<style>"
$a = $a + "BODY{background-color:#D0D0D0;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#CC0000}"
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#D0D0D0}"
$a = $a + "</style>"
$logArray | ConvertTo-HTML -head $a -body "<H2>Leavers Report</H2>"| Out-File "\\*****\Reports\Users_Leaver_Report_$logDate.html " 


##Creates and sends completion email with information##
$emailFrom = "AD-Scripts@*****.com"
$emailTo = "****.Admins@***.com"
$subject = "AD Users Leaver Script Complete"
$smtpServer = "*****"
$body = "The monthly AD Leavers script has run.`r`n"+
"`r`n"+
"`r`n"+
"Report is located \\*****\Reports\User_Leavers_Report_$logDate.html"

Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer $smtpServer

Thanks.
Avatar of youpa

ASKER

This command work fine , however it's only giving the display name, I would like also to have First Name and Lastname.

dsquery user "OU,OUname,DC=domain,DC=local" -limit 0 | dsget user -display -email -pager>c:\Users.csv


thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Kline
Mike Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for the points and glad to help.   Just a heads up next time you can also split points for several helpful answers.

Thanks

Mike
Avatar of youpa

ASKER

Thanks all