1

I've wrote the following code; the purpose of the code is to get the following fields from Active Directory and push it to a CSV file. Below is the code:

# Importing Active Directory Modules
Import-Module ActiveDirectory

# Declaring the File Path of where the Host File will be generated. 
$csvfile = "C:\TEMP\EmployeeFiles.csv"

# Get the only following fields from Active Directory
Get-ADUser -Filter * -Properties Enabled, EmployeeID, GivenName, Surname, title, Company, Manager, BusinessCategory, Mail, Mobile, streetAddress, physicalDeliveryOfficeName, st, co, PostalCode, chpositionID |
    Where-Object {$_.Enabled -eq $True} |
    select EmployeeID, GivenName, Surname, title, Company, Manager,
        BusinessCategory, Mail, Mobile, streetAddress, physicalDeliveryOfficeName,
        st, co, PostalCode, chpositionID |
    Select-Object @{Label = "Employee ID";Expression = {$_.EmployeeID}},
        @{Label = "First Name";Expression = {$_.GivenName}},
        @{Label = "Last Name";Expression = {$_.Surname}},
        @{Label = "Title";Expression = {$_.title}},
        @{Label = "Organisation";Expression = {$_.Company}},
        @{Label = "Supervisor";Expression = {$_.Manager}},
        @{Label = "Business Unit";Expression = {$_.BusinessCategory}},
        @{Label = "Email";Expression = {$_.Mail}};
        @{Label = "Mobile";Expression = {$_.Mobile}},
        @{Label = "Business Address";Expression = {$_.streetAddress}},
        @{Label = "Business City";Expression = {$_.physicalDeliveryOfficeName}},
        @{Label = "Business State";Expression = {$_.st}},
        @{Label = "Business Country";Expression = {$_.co}},
        @{Label = "Business Postcode";Expression = {$_.Postalcode}},
        @{Label = "Position";Expression = {$_.chpositionID}} |
    Export-Csv $csvfile -NoTypeInformation

However, the code runs and generates the CSV file; but the CSV file contains the following information:

"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","2"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Ray
  • 11
  • 1
  • csv means comma separated values. – Kavitha Karunakaran Sep 24 '19 at 06:19
  • Did you try it step-by-step? Does it work, if you only run `Get-AdUser -Filter * .....`? Then does it work if you add the `Where-Object`? Do you get what you expect to this point? Then add the `Select-Object`... And so on.. And I would not `|`everything together. I would `$user=Get-AdUser....`, then add the filter, and so on – Dan Stef Sep 24 '19 at 13:30
  • If you follow what I described in the previous comment, it should work when you have `$user | Select-Object GivenName, Surname | # Export the files as CSV to the File Name and File Path dictate above. Export-CSV $csvfile -NoTypeInformation` at the end... – Dan Stef Sep 24 '19 at 13:43

0 Answers0