Home > Software engineering >  Output on CSV and argument in powershell are not same
Output on CSV and argument in powershell are not same

Time:10-30

I got user information from the user group in AD. every column has no problem except the user name.

On csv, User name is normal but there is a format when I get content from csv for using powershell like as below;

@{Name=abc}

for compare-object with two CSV, I need to use -expand.

Is there anyway to avoid this result?

I want to get a same content on CSV and powershell.

get-adgroup $path -server server.com | get-adgroupmember -recursive | select-object -unique | get-aduser -properties mail | name, mail | export-csv c:\result.csv

CodePudding user response:

Use import-csv cmdlet to import the csv and not get-content. Also the provided code sample won't work - e.g. you missed select-object here:

 | name, mail |

You do not need to query the group, as you already know the name ($path), you can directly query the groupmemberships, e.g.:

get-adgroupmember -identity $path -recursive

But in the end you could achieve the same in a much more efficient way, e.g.:

get-aduser -LDAPFilter "(memberOf:1.2.840.113556.1.4.1941:=[groupDistinguishedName])" -property mail | select-object -property mail,name | export-csv [path]

replace [groupDistinguishedName] with the distinnguishedName of the group. This will give you all users back which are member (transitive) of the defined group.

see: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/4e638665-f466-4597-93c4-12f2ebfabab5

  • Related