Scott Taylor
Scott Taylor

Reputation: 63

Get-ADUser properties in specific order to export into CSV

Semi-Newbie here. I'm trying to use Get-ADUser to display user information in a specific order. It appears Get-ADUser property listing has a default order that is not intuitive for our needs. So far I have;

$Users = Get-ADUser -Filter * -SearchBase $treeview.SelectedNode.Name -Properties Name, CN, SamAccountName, DisplayName, sn, GivenName, Initials, `
OtherName, mail, EmailAddress, EmployeeNumber, Company, StreetAddress, POBox, City, State, PostalCode, Country, Department, HomePhone, `
telephoneNumber, OfficePhone, MobilePhone, Fax, info, physicalDeliveryOfficeName, Title, Office, EmployeeID, Description, DistinguishedName, `
CanonicalName, AccountExpirationDate, Created, Enabled, HomeDirectory, HomeDrive, HomePage, LastLogonDate, LockedOut, logonCount, Manager, `
Modified, msNPAllowDialin, ObjectClass, objectSid, PasswordLastSet, UserPrincipalName, whenChanged | 
Select-Object Name, CN, SamAccountName, `
DisplayName, sn, GivenName, Initials, OtherName, mail, EmailAddress, EmployeeNumber, Company, StreetAddress, POBox, City, State, PostalCode, `
Country, Department, HomePhone, telephoneNumber, OfficePhone, MobilePhone, Fax, info, physicalDeliveryOfficeName, Title, Office, EmployeeID, `
Description, DistinguishedName, CanonicalName, AccountExpirationDate, Created, Enabled, HomeDirectory, HomeDrive, HomePage, LastLogonDate, `
LockedOut, logonCount, Manager, Modified, msNPAllowDialin, ObjectClass, objectSid, PasswordLastSet, UserPrincipalName, whenChanged `
| Export-Csv -NoTypeInformation -Path $LogFile

Although the below code works fairly quickly (4 sec. for 1758 users), this seems a bit long winded and I'm sure there are more efficient methods. I've looked into hash tables but even that is as long as above doubling the effort.

Upvotes: 0

Views: 1028

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25031

If we are to follow Lee_Dailey's advice, you can store the properties you want to filter into an array variable. Then pass that variable into the -Property/-Properties parameters.

$properties = "DisplayName","sn","GivenName","Initials","OtherName","mail","EmailAddress","EmployeeNumber","Company","StreetAddress","POBox","City","State","PostalCode","Country","Department","HomePhone","telephoneNumber","OfficePhone","MobilePhone","Fax","info","physicalDeliveryOfficeName","Title","Office","EmployeeID","Description","DistinguishedName","CanonicalName","AccountExpirationDate","Created","Enabled","HomeDirectory","HomeDrive","HomePage","LastLogonDate","LockedOut","logonCount","Manager","Modified","msNPAllowDialin","ObjectClass","objectSid","PasswordLastSet","UserPrincipalName","whenChanged"

$Users = Get-ADUser -Filter * -SearchBase $treeview.SelectedNode.Name -Properties $properties |
    Select-Object -Property $properties

Upvotes: 2

Related Questions