delete home drives probably simple

I keep banging my head against the wall figuring out why this wont work. from there is a handy script which does exactly what i want. Of course there are some folders in there that i want to be excluded because someone decided to use them for a team.

The script has a handy place to exclude drives $NamesToIgnore = “Jmoody” , but when i use more than one name in there it freaks out. i have tried a comma, semicolon, space, quotes and probably lots of things that i have forgotten and it never gives me the results that i expect.

I just want to delete all of the old home drives from a share for users that no longer exist in AD with a list of exclusions that i can provide.

of course there are users who have had name changes, but i think i have all of those included in my list of exclusions, and besides we back up that share so when the complain i can restore it.

Thank you for the help, i am at the end of my rope!

I have several issues with that script so I rewrote it. The major issue that you’re encountering is that they correctly check $NamesToIgnore the first time with -NotContains but later do -notlike. So they are

  1. Checking this variable twice, once as an array and again as a single item.
  2. Don’t limit the $Folders/Get-Childitem to actual folders.
  3. Gathering -Properties * for no damn reason, never used.
  4. Doing the old make an array and append with += that should go away forever, but so many examples online still lead people to it.
  5. Pauses between each user, for what? The -Whatif should allow you to see it’s removing what you want.

This should handle your needs.

$Folders = get-childitem \\SERVER\SHARE\FOLDER\ -Directory
$NamesToIgnore = "user1","user2","user3"

$folders | foreach {
    if((Get-ADUser -Filter "samaccountname -eq '$($'" -ErrorAction SilentlyContinue) -and $ -notin $NamesToIgnore)
        [PSCustomObject]@{"Folder Name"=$;"Folder Path"=$_.FullName;"Last Write Time"=$_.LastWriteTime}

        Write-Host $
        Remove-Item $_.FullName -Recurse -Force -Verbose -WhatIf
} -OutVariable FoldersToRemove

$FoldersToRemove | Out-GridView

Another thing is if the directory is for a user that’s been deleted out of AD, it will not be removed. I did not change this logic either. Part of continuing is the aduser is able to be found.

Thank you very much, that is a huge help! I sincerely appreciate the quick response. sometimes working for a small company feels like working on an island when there are no co-workers to bounce stuff off of.