Powershell script delete users & home folders

infotech411
infotech411 used Ask the Experts™
on
Hi - I am looking for a powershell script that will delete AD user accounts I specify in a text or csv file and also delete the users home folder.
The scripts I've found online don't do both and then the home folder deletion scripts only work on 1 file server, but I have multiple servers to cleanup.

Does anyone have a script that will do this?  The home folder script could be separate, I could run one that looks at the home folder and if no ad user account is found then it would delete the home folder.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The following is a great article about adding users to AD with powershell. It shows you how you can modify the script for adding other attributes to customize the script on your own. Highly recommended

http://www.howtogeek.com/50205/adding-more-fields-to-user-creation-with-powershell/

This is part 2 in a series of articles. You can use part 1 (linked as "previous article") to get the script and use the info in part 2 to customize it.


Good Luck!
The script below takes a txt file full of usernames, and loops through them one at a time. for each user in the file, it will remove the folder that is set as their home directory, then remove the AD account.

I've only done a tiny bit of testing with this, but it seems to work fine in my test lab.

Hope it helps...

Import-Module ActiveDirectory

$users = Get-Content users.txt

foreach ($user in $users) {

    Remove-Item $user.HomeDirectory
    Remove-ADUser $user.samAccountName -Confirm:$False

}

Open in new window

whoops, i missed a line out. Sorry...

Import-Module ActiveDirectory

$users = Get-Content users.txt

foreach ($user in $users) {

    $aduser = Get-ADUser $user -Properties HomeDirectory

    Remove-Item $aduser.HomeDirectory
    Remove-ADUser $aduser.samAccountName -Confirm:$False

}

Open in new window

Author

Commented:
Thanks, what is the name format supposed to be in the text file?
Go for the user logon name.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial