简体   繁体   中英

PowerShell - Filter Get-ADUser to get disabled accounts only

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM