0
votes

I have a csv file which contains a list of user names with no header. I want to end up with a second csv file which contains ONLY those user names that correspond to Active Directory users who are disabled.

I was playing around with a Get-Content and then ForEach-Object (%) loop to query the AD and return each SAM and the status of it's enabled property.

Get-Content users.csv | % {Get-ADUser $_ | Select-Object samaccountname,enabled}

Although what I really need to do is look at each name and move ONLY the ones tied to disabled accounts to a new csv file. Only I cant quite sort out how to accomplish this.

1

1 Answers

1
votes

you're almost there:

$users = @(gc users.csv | % {Get-ADUser $_ | select samaccountname,enabled})
$users | ? { -not($_.enabled) } | export-csv -notypeinformation $somecsv

what we're doing:

  1. Loop through the text file with the foreach-object loop and select your desired AD user attributes and assign them to an array @(). They normally would return as an array but I always explicitly cast because if ONE result is returned, it's returned as a single object instead of a one-entry array.
  2. Then we filters those users using the where-object alias (?) that are not enabled and then send those to your "disabled_users.csv"