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.

5
  • ... | 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") Commented Oct 18, 2022 at 19:55
  • What column names do you want your Csv to have? Commented Oct 18, 2022 at 20:16
  • @santiagoSquarzon hi, “Name” and “Status” should be fine. Commented Oct 18, 2022 at 20:34
  • So you want only those members which are "users" correct? you're not interested in members of a different ObjectClass ? Commented Oct 18, 2022 at 20:36
  • @santiagosquarzon yes, correct. Commented Oct 18, 2022 at 20:42

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Santiago,it work thank you so much. By anychance could you please help me with this too stackoverflow.com/questions/74119092/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.