none
Finding AD accounts that are disabled...... RRS feed

  • Question

  • I'm back again......

    I am trying to take a csv from Office 365 that shows the LICENSED UPN's and then split that to get the samaccountname and then run that through get-aduser, using where-object to find the ones that are disabled. Here is my script so far....

    $Users = Import-csv c:\emails365.csv

    Foreach($User in $Users)
    {
        $SAM = ($User.UserPrincipalName -split "@")[0] 
        $SAM | get-aduser| where-object{$_.Enabled -eq 'False' }|Select Name | Sort Name

    }

    It seems as though it is just outputting the $SAM variable though. At least the split part is working I guess. And not in fact outputting the disabled accounts in AD from the csv.

    Any help is appreciated.

    Matthew

    Wednesday, May 6, 2015 11:38 PM

Answers

  • Hi Matthew,

    Here's a starter:

    Import-Csv .\input.csv | ForEach {
    
        $user = Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'"
    
        If ($user.Enabled -eq $false) {
    
            $user
    
        }
    
    } | Export-Csv .\disabledUsers.csv -NoTypeInformation

    This assumes your input CSV has a header of UserPrincipalName.


    EDIT: If you really do need to use the SamAccountName instead of the UPN:

    Import-Csv .\input.csv | ForEach {
    
        $user = Get-ADUser -Filter "SamAccountName -eq '$(($_.UserPrincipalName -split '@')[0])'"
    
        If ($user.Enabled -eq $false) {
    
            $user
    
        }
    
    } | Export-Csv .\disabledUsers.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Edited by Mike Laughlin Thursday, May 7, 2015 12:50 AM
    • Marked as answer by MatthewMM Thursday, May 7, 2015 3:10 PM
    Thursday, May 7, 2015 12:08 AM

All replies

  • Hi Matthew,

    Here's a starter:

    Import-Csv .\input.csv | ForEach {
    
        $user = Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'"
    
        If ($user.Enabled -eq $false) {
    
            $user
    
        }
    
    } | Export-Csv .\disabledUsers.csv -NoTypeInformation

    This assumes your input CSV has a header of UserPrincipalName.


    EDIT: If you really do need to use the SamAccountName instead of the UPN:

    Import-Csv .\input.csv | ForEach {
    
        $user = Get-ADUser -Filter "SamAccountName -eq '$(($_.UserPrincipalName -split '@')[0])'"
    
        If ($user.Enabled -eq $false) {
    
            $user
    
        }
    
    } | Export-Csv .\disabledUsers.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Edited by Mike Laughlin Thursday, May 7, 2015 12:50 AM
    • Marked as answer by MatthewMM Thursday, May 7, 2015 3:10 PM
    Thursday, May 7, 2015 12:08 AM
  • Beautiful, thanks for the lesson.
    Thursday, May 7, 2015 3:11 PM
  • This is faster and uses the indexed property rather than a filter on SamName.  It also only retrieves disabled accounts which simplifies the code.

    Either will work but this one should be a bit faster.


    \_(ツ)_/

    Thursday, May 7, 2015 3:54 PM
  • This is faster and uses the indexed property rather than a filter on SamName.  It also only retrieves disabled accounts which simplifies the code.

    Either will work but this one should be a bit faster.


    \_(ツ)_/

    jrv - looks like the editor ate your code again.

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Thursday, May 7, 2015 4:08 PM
  • That is interesting.

    \_(ツ)_/

    Thursday, May 7, 2015 4:09 PM
  • This is faster and uses the indexed property rather than a filter on SamName.  It also only retrieves disabled accounts which simplifies the code.

    Import-Csv .\input.csv | 
        ForEach {
        	Get-ADUser ($_.UserPrincipalName -split '@')[0] -Filter {Enabled -eq $false}
        } | 
       Export-Csv .\disabledUsers.csv -NoTypeInformation
    

    Either will work but this one should be a bit faster.


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Thursday, May 7, 2015 4:14 PM
    Thursday, May 7, 2015 4:11 PM