MMbill MMbill - 3 months ago 71
PowerShell Question

Export to CSV only returning string length

I'm trying to get this script to export to a CSV file, it only lists the string length and not the emails i am trying to pull.

Get-ADGroup -filter {name -like 'Security Group'} |
Get-ADGroupMember -Recursive |
Get-ADUser -Properties Mail |
select -ExpandProperty Mail |
Export-Csv -NoType MyCSVfile1.csv

Answer

Export-Csv expects to receive an object, you've given it a string so it's giving you the properties of that string in the output file (that is, Length).

Drop -ExpandProperty and it will be fine.

Get-ADGroup -filter {name -like 'Security Group'} |
    Get-ADGroupMember -Recursive |
    Get-ADUser -Properties Mail |
    Select Mail |
    Export-Csv -NoType MyCSVfile1.csv