Solved

Deleting Home Directories along with User in AD

Posted on 2014-09-12
2
237 Views
Last Modified: 2014-09-12
I am trying to make a change to this script so that i can go one level up in the home directory and delete from there. The homedirectory property in AD is formatted like this \\server01\staff\%username%\documents. I am looking to delete the path of \\server01\staff\"username". The script currently deletes the documents and not the folder labeled as the username. Here is the script.
[embed=snippet 8264701]

Open in new window

Import-Module ActiveDirectory
$Users = Import-Csv C:\Update\deleteusers.csv

Foreach ($User in $Users) {
Get-ADUser -identity $User.Username -properties Name, homedirectory | % {
    Write-Host ("Removing homdir for:" + $_.Name + ",path:" + $_.homedirectory)
    rm -Force -Recurse $_.homedirectory

}
Remove-ADuser $User.Username
}

Open in new window

0
Comment
Question by:falconcurt
2 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40319582
The following should work for you.  It uses Split-Path to get the parent directory.
Import-Module ActiveDirectory
$Users = Import-Csv C:\Update\deleteusers.csv

Foreach ($User in $Users) {
Get-ADUser -identity $User.Username -properties Name, homedirectory | % {
    $home = Split-Path $_.homedirectory -Parent
    Write-Host ("Removing homdir for:" + $_.Name + ",path:" + $home)
    rm -Force -Recurse $home

}
Remove-ADuser $User.Username
}

Open in new window

0
 

Author Comment

by:falconcurt
ID: 40319807
That did it. I did have one hiccup regarding deleting a leaf object but i used the remove-adobject cmdlet instead....with the -recursive parameter and all went well. Thanks for your response footech. Here was my outcome
Import-Module ActiveDirectory
$Users = Import-Csv C:\Update\deleteusers.csv

Foreach ($User in $Users) {
Get-ADUser -identity $User.Username -properties Name, distinguishedname, homedirectory | % {
    $homepath = Split-Path $_.homedirectory -Parent 
    Write-Host ("Removing homdir for:" + $_.Name + ",path:" + $homepath)
    rm -Force -Recurse $homepath
    Remove-Adobject $_.distinguishedname -recursive -Confirm:$false
}
}

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now