Welcome › Forums › General PowerShell Q&A › delete home drives probably simple
- This topic has 2 replies, 2 voices, and was last updated 4 months ago by
Participant.
-
AuthorPosts
-
-
September 16, 2020 at 6:13 pm #257102
I keep banging my head against the wall figuring out why this wont work. from https://deployhappiness.com/delete-unused-home-folders-with-powershell/ 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!
-
September 16, 2020 at 6:48 pm #257111
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.
PowerShell123456789101112131415$Folders = get-childitem \\SERVER\SHARE\FOLDER\ -Directory$NamesToIgnore = "user1","user2","user3"$folders | foreach {if((Get-ADUser -Filter "samaccountname -eq '$($_.name)'" -ErrorAction SilentlyContinue) -and $_.name -notin $NamesToIgnore){[PSCustomObject]@{"Folder Name"=$_.name;"Folder Path"=$_.FullName;"Last Write Time"=$_.LastWriteTime}Write-Host $_.nameRemove-Item $_.FullName -Recurse -Force -Verbose -WhatIf}} -OutVariable FoldersToRemove$FoldersToRemove | Out-GridViewAnother 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.
-
September 17, 2020 at 4:11 pm #257237
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.
-
-
AuthorPosts
- The topic ‘delete home drives probably simple’ is closed to new replies.