Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Deleting Home Directories along with User in AD

Posted on 2014-09-12
2
Medium Priority
?
255 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 41

Accepted Solution

by:
footech earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

886 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