Search code examples
powershellactive-directorywindows-server-2012windows-server-2012-r2windows-server

Identify home folders for user accounts that have been deleted


I have a large folder E:\Users that has a folder for each user by their logon name for example E:\Users\jt10192.

I want to loop through every folder in E:\Users and get a list of home folders that belonged to users that have since been deleted.

This is how far I've got, but I'm not sure what to run to lookup if $folder is a user and output something if not.

$folders = Get-ChildItem | Where-Object { $_.PSIsContainer } | Select-Object Name

foreach ($folder in $folders) {

       // lookup if $folder is a user and output something if not...

}

Solution

  • If you're using AD to lookup your users accounts:

    $path = "E:\Users"
    $folders = Get-ChildItem $path -Directory
    
    ForEach ($folder in $folders) {
        If(Get-ADUser -Filter {sAMAccountName -eq $($folder.name)}){
            Write-Host "Found matching User for: $($folder.FullName)" -ForegroundColor Green
        }
        else {
            Write-Host "No account found for folder: $($folder.FullName)" -ForegroundColor Red
        }
    }