0

Is it possible to delete an AD User and its associated folders? The folder structure is:

D:\Users\Profiles
D:\Users\Redirect
D:\Users\Data

I am trying to use the following script which deletes users that have not logged in within 90 days. This is NOT what I want.

function Delete-ADUser
{
    Param($userName = $(throw 'Enter a username to delete'))
    $searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcategory=user)(sAMAccountName=$userName))")
    $user = $searcher.findone().GetDirectoryEntry()
    $user.psbase.DeleteTree()
}


$NumDays = 90
$LogDir = ".\Removed-User-Accounts.log"

$currentDate = [System.DateTime]::Now
$currentDateUtc = $currentDate.ToUniversalTime()
$lltstamplimit = $currentDateUtc.AddDays(- $NumDays)
$lltIntLimit = $lltstampLimit.ToFileTime()
$adobjroot = [adsi]''
$objstalesearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
$objstalesearcher.filter = "(&(objectCategory=person)(objectClass=user)(lastLogonTimeStamp<=" + $lltIntLimit + "))"
$users = $objstalesearcher.findone()

Write-Output `n`n"----------------------------------------" "ACCOUNTS OLDER THAN "$NumDays" DAYS" "PROCESSED ON:" $currentDate "----------------------------------------" `
| Out-File $LogDir -append

if ($users.Count -eq 0)
{
       Write-Output "  No account needs to be removed." | Out-File $LogDir -append
}
else
{
       foreach ($user in $users)
       {
              # Read the user properties
              [string]$adsPath = $user.Properties.adspath
              [string]$displayName = $user.Properties.displayname
              [string]$samAccountName = $user.Properties.samaccountname
              [string]$lastLogonInterval = $user.Properties.lastlogontimestamp

              # Delete the user
              Delete-ADUser $samAccountName

              # Convert the date and time to the local time zone
              $lastLogon = [System.DateTime]::FromFileTime($lastLogonInterval)

              Write-Output "  Removed user " $displayName" | Username: "$samAccountName" | Last Logon: "$lastLogon"`n" `
              | Out-File $LogDir -append
       }
}

Question: How do I modify the script that will ask me for a Username to Enter and delete its folders ?

4
  • What is the relationship between the domain user and those folders?
    – evilSnobu
    Oct 21, 2014 at 13:33
  • @kamikatze Those users are domain users. I could go and manually delete the user and then their folders but I was thinking to do it on one go. Oct 21, 2014 at 13:48
  • Where are those folders located? On a client computer? On some network share?
    – evilSnobu
    Oct 21, 2014 at 13:55
  • @kamikatze these folders are shared folders, so \\addsServer\$Profiles\%username% and so on Oct 21, 2014 at 13:58

2 Answers 2

1

Assuming you are running this on "addsServer", so $profilePath is local to the machine, and D:\Profiles\username stores the user profile.

$GoodbyeList = 'JDoe', 'KDoe', 'LDoe'
$profilePath = 'D:\Profiles'

foreach ($user in $GoodbyeList) {
    Remove-ADUser -Identity $user
    Remove-Item "$profilePath\$user" -Recurse -Force -Verbose
}
2
  • works nicely! Is it possible to provide the username as parameter? Oct 21, 2014 at 15:18
  • Just for future reference: Replace the first line with: param ([Parameter(Mandatory=$true)] [string[]]$GoodbyeList)
    – evilSnobu
    Oct 21, 2014 at 15:51
0

if your users do not share common root location for home directories, you can query home directory attributes for each user before deleting folders

foreach ($user in $GoodbyeList) { Remove-ADUser -Identity $user $homeDirectory = (Get-ADUser $user -Properties homeDirectory | Select-Object -ExpandProperty homeDirectory) Remove-Item "$homeDirectory" -Recurse -Force -Verbose }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .