Search code examples
powershellcsvactive-directoryget-aduser

Removing the header in Powershell Export-Csv output for Select-Object


I'm outputting a list of members from an AD group to a csv file. Right now it's outputting with the header 'name'. I currently want to have the output without the header.

This is my code:

$groupNames = @('Group1', 'Group2', 'Group3', 'Group4')

foreach ($group in $groupNames) {
    $csvPath = "$group.csv"
    $groupdn = (Get-ADGroup $group).DistinguishedName
    Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
        Select-Object name |
        Export-Csv -Path $csvPath -NoTypeInformation
}

Current Output:

name
clark kent
mickey mouse
jolly rancher

Desired Output:

clark kent
mickey mouse
jolly rancher

I've tried replacing Select-Object -Property name with Select-Object -ExpandProperty name but it does not give the desired output found in other answers on here.


Solution

  • What you are looking for is no longer a Csv but a plain text file with a list of AD object names thus Export-Csv is no longer the right cmdlet for your use case, you should change it to Set-Content and expand the objects .Name attribute instead of selecting them:

    $groupNames = @('Group1', 'Group2', 'Group3', 'Group4')
    
    foreach ($group in $groupNames) {
        $csvPath = "$group.csv"
        $groupdn = (Get-ADGroup $group).DistinguishedName
        Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
            ForEach-Object name |
            Set-Content $plainTextPath
    }