Home > Back-end >  How to remove @{} tag from object output
How to remove @{} tag from object output

Time:04-05

I am running into issues with a script to list users and groups that each user is "member of" in a CSV file. I've tried numerous ways of exporting these objects and can't figure out how to remove the @{} tag in my output.

My output is:

@{Name=Jane Doe}

@{Group=Regional OU Admins}

Here is my code:

$OUPath = “OU=Computers, OU=HQ,OU=USA,DC=STACKOVERFLOW,DC=COM”
$users = (get-aduser -Filter * -SearchBase $OUpath  | select SamAccountName)


ForEach ($User in $Users)
    {   $Username = Get-ADUser $($user.SamAccountName) -Properties MemberOf
        $Groups = ForEach ($Group in ($Username.MemberOf))
        {   (Get-ADGroup $Group).Name
        }
        $Groups = $Groups | Sort
        ForEach ($Group in $Groups)
        {  New-Object PSObject -Property @{
                 Name = $Username.Name
                 Group = $Group
            } | Select name,group | Add-Content C:\Temp\test.csv

            
            
        }
    }

What am I doing wrong here?

CodePudding user response:

You're writing the objects to the TXT file. You'll have to format your output before writing it this way, but there's a better way to do what (it seems like) you're trying to do:

ForEach ($User in $Users) {
    $Username = Get-ADUser $($user.SamAccountName) -Properties MemberOf
    $Groups = ForEach ($Group in ($Username.MemberOf)) {
      (Get-ADGroup $Group).Name
    }
    if ($null -eq $Groups) {
        continue;
    }
    $Groups | Sort | Select-Object @{ "Name" = "Group"; "Expression" = { $_ } }, @{ "Name" = "Name"; "Expression" = { $Username.Name } } | Export-Csv "D:\temp\$($Username.Name).csv" -Encoding utf8
}

Note that this script will output 1 CSV file per user, but you can easily aggregate the results and pipe it to a single Export-Csv!

Edit

As requested, this is a variation aggregating all the information into a single CSV file. This differs a little from your initial implementation. This is the best way I can think of on how to do it:

$users = Get-ADUser -LDAPFilter "(sAMAccountName=vi*)" -Properties @("MemberOf");

$result = New-Object "Collections.Generic.List[Object]";
foreach ($user in $users) {
    if ($null -eq $user.MemberOf) { continue; }
    $memberOf = $user.MemberOf | Get-ADGroup | Select-Object -ExpandProperty Name;
    $memberOf | ForEach-Object { $result.Add((New-Object PSObject -Property @{
        Name = $user.Name
        Group = $_
    }))};
}
$result | Sort-Object Name | Export-Csv D:\temp\output.csv -Encoding utf8;

CodePudding user response:

If you're only interested in the group's Common Name you could get your extract with some string manipulation, without the need to query the groups each user is a member of:

$re = [regex]'(?i)(?<=^cn=)(.*?)(?=,(ou|dc|cn)=)'
$param = @{
    SearchBase = "OU=Computers,OU=HQ,OU=USA,DC=STACKOVERFLOW,DC=COM"
    Properties = 'MemberOf'
    Filter     = '*'
}

Get-ADUser @param | ForEach-Object {
    foreach($group in $_.MemberOf) {
        [pscustomobject]@{
            Name  = $_.Name
            Group = $re.Match($group).Value
        }
    }
} | Export-Csv C:\Temp\test.csv -NoTypeInformation
  • Related