Solved

Deleting Home Directories along with User in AD

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In-place Upgrading Dirsync to Azure AD Connect
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

828 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