1

I have a simple script and I'm trying export csv file with only two columns $findUser.Name and $userStatus but I'm not sure why it's not exporting the value for those two.

Any help or suggestion would be really appreciated.

$groupdMember = Get-AdGroupMember -Identity $GroupName

foreach($member in $groupdMember){

    $findUser = Get-ADUser -Identity $member.Name

    if($findUser.Enabled -eq "False"){
        $userStatus = "Disabled"
    }else{
        $userStatus = "Enabled"
    }

   Write-Host $findUser.Name, $userStatus
   $member | Select-Object $findUser.Name, $userStatus | Export-Csv -Path E:\scripts\aduser-report.csv -Append -notypeinformation -force
} 

If I removed the Select-Object then it able to export it but it's not what I wanted. I just wanted to export a csv file that have only $finduser.Name and $userStatus.

aasenomad
  • 405
  • 2
  • 11
  • ```... | Select-Object @( "Name", "Enabled" ) | ...```, or more simply ```... | Select-Object Name, Enabled | ...```. The parameters need to be the *names* of the properties, not the their *values* - at the moment you're telling it to select the properties *called* literally "My User XYZ" (or whatever the user's name is) and "Disabled" or "Enabled" (which will work when the user is enabled because the property is *called* "Enabled", but it won't work when the user is "Disabled") – mclayton Oct 18 '22 at 19:55
  • What column names do you want your Csv to have? – Santiago Squarzon Oct 18 '22 at 20:16
  • @santiagoSquarzon hi, “Name” and “Status” should be fine. – aasenomad Oct 18 '22 at 20:34
  • So you want only those members which are "users" correct? you're not interested in members of a different `ObjectClass` ? – Santiago Squarzon Oct 18 '22 at 20:36
  • @santiagosquarzon yes, correct. – aasenomad Oct 18 '22 at 20:42

1 Answers1

1

You can use a calculated property with Select-Object to create the desired objects, the expression (E={...}) checks if the Enabled property of each user is $true or $false and returns accordingly. Since Get-ADGroupMember can return objects other than user, it's better to just query for any user having the target group in their memberof attribute:

$group  = 'someGroup'
$filter = '(memberof={0})' -f (Get-ADGroup $group).DistinguishedName
Get-ADUser -LDAPFilter $filter |
    Select-Object Name, @{N='Status'; E={ if($_.Enabled) { return 'Enabled' } 'Disabled' }} |
    Export-Csv path\to\outpout.csv -NoTypeInformation
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    Hi Santiago,it work thank you so much. By anychance could you please help me with this too https://stackoverflow.com/questions/74119092/how-to-speed-up-the-process-in-powershell – aasenomad Oct 19 '22 at 01:56