delete user account and associated redirect folders

We have an environment with around 2000 users (GPO with desktop and my docs redirected to the server) and every year we have a few hundred users leaving. Is there a way we can delete the user accounts and associated redirected folders at once?

Thanks in advance
LVL 1
jct_777Asked:
Who is Participating?

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

x
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.

Benjamin VoglarIT ProCommented:
jct_777Author Commented:
Thanks Benjamin. Not what I was looking for.
Lionel MMSmall Business IT ConsultantCommented:
So do you want to be able to have script, enter a username, take ownership of those files, and then delete them? And then also have them deleted from AD? What about shares associated with users?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

jct_777Author Commented:
Yes Lionelmm. No shares associated with users
Lionel MMSmall Business IT ConsultantCommented:
step 1 remove the user permissions
icacls Y:\Redirected\Folder2Delete /reset
icacls Y:\Redirected\Folder2Delete /remove:g "domain\user-name /t /q
step 2 take ownership
takeown Y:\Redirected\Folder2Delete /r /d:n
step 3 delete folder
rd Y:\Redirected\Folder2Delete /s/q
step 4 remove user from AD
net user user-name /delete

If all the above is what you want done then I/we can put it into a script where you can either enter the username manually, get prompted for one, or put it into a text file for the script to read when it run. Will above work and if so what approach do you want to take?
jct_777Author Commented:
Hi Lionelmm,

Looks good. I'd rather put it into a text file for the script to read when it run.

Thanks
Lionel MMSmall Business IT ConsultantCommented:
OK so only missing piece is how, what variable do we need to know to be able to know which folders to delete, that is associated with each user's name. In other words what is the variable for Y:\Redirected\Folder2Delete I assume it is related to their username somehow so is it  Y:\Redirected\%username%?
Lionel MMSmall Business IT ConsultantCommented:
This has not been tested yet so do on a test user and folder first. Assuming the list of users to delete is called users.txt. Create batch, let's say called StartDeleting.Bat and put this in it
for /f %a in (C:\EE\users.txt) do (call C:\EE\DeleteUser.bat %a)
now add the code to C:\EE\DeleteUser.bat file and save
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

Rem Set Variables
Rem      Add Folder Parameters
Set DelFolder=Y:\Redirected\
Rem	Set Admin User that will take ownership
Set AdminName=Bob
Rem      Add your Domain here
Set Domain=YourDomain

pushd "%DelFolder%"

Rem      Take Owership of the Directory for the User (not required)
takeown %DelFolder%\%%a /r /d:n

REM	Disable inheritance
icacls "%RootDir%"\%%a /inheritance:d

Rem     Remove existing permissions and replace with new
Rem	You may be able to skip this step and be allowed to RD folders
icacls "%RootDir%"\%%a /reset
icacls "%RootDir%"\%%a /inheritance:r /grant:r %YourDomain%\%AdminName%:^(OI^)^(CI^)F /T

Rem	Delete existing folders
"%RootDir%"\%%a /s/q

Rem	Remove user from AD
net user %%a /delete

:end
popd

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
Lionel MMSmall Business IT ConsultantCommented:
Glad to have helped--did it work as you wanted it to?
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
Microsoft Legacy OS

From novice to tech pro — start learning today.