Home > Net >  how to export csv file using PowerShell
how to export csv file using PowerShell

Time:10-19

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.

CodePudding user response:

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
  • Related