Search code examples
powershellazure-active-directorysystem-administrationactive-directory-group

Active Directory Powershell : Export-Csv is missing out some data


I am very new to Powershell and I'm trying to export three attributes found in multiple groups. Lets say these are Attribute A, Attribute B, and Attribute C.

Attribute A and B are present in all groups returned by the Get-ADGroup query I've written. However, Attribute C is only present in 1/3 of the groups, and for the remainder of these groups the Attribute C field is 'null'.

When I try to export this data to excel (using Export-Csv), Attribute A and B are correctly exported as columns, however Attribute C is not there. How do I include Attribute C as a column for the groups that have that field filled in, when exporting this query results as csv?

The query that I am using is:

Get-ADGroup -LDAPFilter "(name=IT-*)" -SearchScope Subtree -SearchBase "DC=KRFT, DC=Net" 
-Properties Attribute A, Attribute B, Attribute C | Export-Csv 
"C:\Users\user1\Desktop\Powershell\groups.csv" 

Thanks :)


Solution

  • As commented, you can force the columns that appear in the output CSV by inserting a Select-Object before the Export-Csv cmdlet. That way, all items written will have this column, empty or not:

    Get-ADGroup -LDAPFilter "(name=IT-*)" -SearchScope Subtree -SearchBase "DC=KRFT,DC=Net" -Properties AttributeA, AttributeB, AttributeC | 
        Select-Object AttributeA, AttributeB, AttributeC | Export-Csv "C:\Users\user1\Desktop\Powershell\groups.csv"