简体   繁体   中英

Powershell Active Directory script to export csv with specific information/attributes

I need to export some information from active directory. The current powershell script successfully get all the information it is asking for, but I want to also grab the user attribute "description" or "company."

$OUpath = 'OU=OU,DC=DC'
$ExportPath = 'C:\path\users_in_ou7.csv'
Get-ADUser -Filter * -SearchBase $OUpath | Select-object GivenName, Surname,Name,UserPrincipalName | Export-Csv -NoType $ExportPath

When I add either of those to the Select-object portion is turns up blank in my CSV. For example:

Select-object Description, Company, GivenName, Surname, Name, UserPrincipalName

Column headers are inserted into the CSV, but the values are blank. These attributes are populated in each of the user properties in AD. I am not sure if I am calling them correctly in my script. Any help would be appreiceiated. Thank you.

Use the -Properties parameter of the Get-ADUser cmdlet.

From the Get-ADUser documentation :

This cmdlet retrieves a default set of user object properties. To retrieve additional properties use the Properties parameter.

Specify properties for this parameter as a comma-separated list of names. To display all of the attributes that are set on the object, specify * (asterisk).

You add the -properties * to the query to extract all attributes.

Get-ADUser -Filter * -SearchBase $OUpath -properties * | Select-object Description,GivenName, Surname,Name,UserPrincipalName | Export-Csv -NoType $ExportPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM