Delete Local Profiles via Disabled AD Accounts

Hey guys,

My programming knowledge lacks greatly. Curious if there is a batch script out there that would help greatly improve efficiency.

We have A huge number of employees that come and go. They are added to AD and then are disabled when they are terminated. The problem is, we have three "RDP" machines that all of these users get on. So when they log in Windows creates a local profile for them. Well now space is an issue and one of the ways we have saved quite a bit is deleting all of the disabled termed local profiles. The problem is currently I have to open the Domain controller on one screen and have the RDP machine on the other and look at each one, then delete. It takes a TON of time and i have to do it on every RDP machine considering they are not a roaming profile.

So my question is, is there a script that will look into your AD tree and find all of the Disabled accounts and then delete them on the computer that has the local profiles? Or vice versa, look at all of the local profiles, look them up in AD and if they are disabled, delete the profile? I can't imagine this is the only way to do this. Thoughts?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What OS are the RDP machines running?
What OS are the DCs?
sivtecAuthor Commented:
Hey Footech,

The RDP's are all running Windows Server 2008 R2. The DC is also Windows Server 2008 R2.
The following makes use of one of the AD cmdlets, so those have to be available (meaning you either need to run on the DC, or have the RSAT tools installed).
# This works on WinVista+
Import-Module ActiveDirectory
$complist = "C:\temp\computerlist.txt"  #Put the names of your RDP servers in this file
$reviewFile = "C:\temp\reviewBeforeDelete.csv"
Search-ADAccount -AccountDisabled -UsersOnly -SearchBase "OU=myOU,DC=domain,DC=com" | Select samAccountName,SID | Export-Csv $reviewFile -NoTypeInformation
#As a safety check review the contents of the file created by the previous command, and delete any lines that you don't want removed.

$UserSIDs = Import-Csv $reviewFile | ForEach { $_.SID } 

Get-Content $complist | ForEach {
    #retrieve profiles for matching users on the target machine
    $userProfiles = Get-WmiObject Win32_UserProfile -ComputerName $_ | Where { $UserSIDs -contains $_.SID }
    # Test if profile exists and if so, remove it
    If ($userProfiles)
        foreach ($userProfile in $userProfiles)
            Write-Output "Removed $($userProfile.LocalPath) from $_."
    Remove-Variable userProfiles

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

David Johnson, CD, MVPOwnerCommented:
group policy
Group Policy setting. Under Computer Configuration ' Administrative Templates ' System ' User Profiles  'Delete user profiles older than a specified number of days on system restart'
sivtecAuthor Commented:
Thanks Foottech. I think i will go test that in the Microsoft labs see if it's what I need, but that is a great start.

Thanks David for the idea however, that won't work because some employees don't log on to the RDP's for awhile even if they aren't termed but that's a good idea nonetheless.
The script provided in https:#a40980923 meets the requirements asked for:
1) look into your AD tree and find all of the Disabled accounts
2) and then delete them on the computer that has the local profiles

Always nice to get confirmation from the asker, but there's no doubt the script works as I've used it, and so the post should be accepted as the answer (and I believe there's some actual value in not deleting the question).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.