Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting Home Directories along with User in AD

Posted on 2014-09-12
2
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

670 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