Solved

Deleting Home Directories along with User in AD

Posted on 2014-09-12
2
233 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

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

744 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

12 Experts available now in Live!

Get 1:1 Help Now