Script to delete home folders for list of users

rated by 0 users
This post has 9 Replies | 2 Followers

Top 25 Contributor
Posts 123
JakeS Posted: 04-17-2012 2:02 PM

I am trying to find a way to modify the script below or if anyone has a script that will delete home folders for a list of users if the user names are specified in a text or csv file, can anyone help?

I would like to delete the user accounts as well, but the home folders is fine for now.  I want to do this for users in a specific ou and they have a stamped attribute on them from the Quest ActiveRole server, if i could query for users that have the attribute which is a date value, then return the list of users and delete the home folders if the date is within a specific time frame, IE: 7 days, then that would be ideal.

The starting script I have will delete a folder for a single user:

$psSession = New-PSSession -ComputerName HyperV -Credential nwtraders\administrator 

Invoke-Command -Session $PSSession -ScriptBlock {
  $HD=$PD=$null
  $Filter = "(&(ObjectCategory=user)(Name=helpme testuser))"
  $Searcher = [adsiSearcher]($Filter)
  $Searcher.Findall() | 
    ForEach-Object { 
      $HD = $_.properties.homedirectory 
      $PD = $_.properties.profilepath
      $PD = "$PD.V2"
   } #end foreach-object
} #end scriptblock

"Taking ownership of $hd"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "takeown /f $HD /r /d y" } 
"Taking ownership of $pd"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "takeown /f $PD /r /d y" }
"Adding Administrator Rights to $hd"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "icacls $HD /grant administrators:F /t" }
"Adding Administrator Rights to $PD"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "icacls $PD /grant administrators:F /t" }
"Removing folder $hd"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "Remove-Item -Path $HD -Recurse -Force" }
"Removing Folder $PD"
  Invoke-Command -Session $PSSession -ScriptBlock `
    { Invoke-Expression "RD $PD -Recurse -Force" }

Remove-PSSession -Session $psSession

Top 10 Contributor
Posts 640

Try re-posting this in the AD forum. PowerShell for Active Directory

Top 25 Contributor
Posts 123

Thank you Don again!  Also, I have your books and learning materials, they are excellent!  Hopefully I will be answering questions soon :)

 

 

Top 25 Contributor
Posts 123

Hi Don,

I had tried that, but no luck over there.  I am giving up on deleting the users, right now I just want to get this script working to take a list of usernames from a file and then go delete the home folders.

Is that something you can point me in the right direction on?  If not, no problem, thanks as always!

Also, if you have any suggestions on how to just get good at powershell, let me know.  I can fix about any problem, build a network, firewalls, routers, servers, windows, unix, but when it comes to scripting, I just never got into it and it sucks having not done so :)

I don't know any languages, so I'm starting from scratch really.  I'm open to books, videos, whatever you suggest.  I have some stuff but I get lost in the books a bit as I am not sure how to get from point a to point b in a script.

 

Top 10 Contributor
Posts 640

"Getting good" - well, you could buy my book if you haven't :). http://MoreLunches.com. Right now, you're dealing with the fact that a lot of that stuff - network, firewalls, routers, etc - hasn't been enabled in PowerShell. That's finally coming in Windows 8 and Windows Server 2012. I also did training videos at http://videotraining.interfacett.com, so you might give those a look as well. 

So, assume you have a file servers.txt which lists one file server UNC per line. That would be something like \\SERVER\D$ - whatever path you can point to where each subfolder is a user's home folder (e.g., \\SERVER\D$\DonJ). I'm assuming individual user home folders aren't shared - you'd need to delete the share first, and that's a bit tricky pre-PowerShell v3. Also assume users.txt contains one user name per line.

Get-Content users.txt | ForEach-Object {
 $user = $_
 Get-Content servers.txt | ForEach-Object {
   Remove-Item -Path (Join-Path -Path $_ -ChildPath $user)
 }
}

Something along those lines. Assuming you have all the permissions and whatnot, it'll go through the list of users, and for each user go through the list of servers, and for each server attempt to delete the specified path. Obviously your environment might differ from my expectations, and so this might need some tweaking or even re-thinking. It's tough to just hand out answers when I'm not sitting at your desk with all that info in front of me, so I try to hand out a sort of template and explain it, and hope that you can go on from there.

But stop thinking of PowerShell as a scripting language. It's a command-line shell. It contains a scripting language, but you don't have to dive right into that first. The whole point of my Lunches book is to teach you how the shell works - don't worry too much about "point a to point b;" learn the shell first, and the rest will follow more naturally. 

Top 25 Contributor
Posts 123

Hi Don,

Thanks, I did see the book you just referenced, I was going to try to ask you on PM or something, but regardless, I bought the Sapien books/training videos, and I thought that you were the author or co-author of their powershell books, is that you or someone else? 

But since you suggested your new book, I am ordering it today.  I'll look at the videos as well, money is tight right now, but if possible I'll see if I can get them as well.

I'll keep working on this script, thanks again.  Your suggestion makes sense.  I want to query the user account specified in the file, determine what their home folder path is, then go delete the home folder.

 

Top 25 Contributor
Posts 123

Hi Don,

Thanks, I did see the book you just referenced, I was going to try to ask you on PM or something, but regardless, I bought the Sapien books/training videos, and I thought that you were the author or co-author of their powershell books, is that you or someone else? 

But since you suggested your new book, I am ordering it today.  I'll look at the videos as well, money is tight right now, but if possible I'll see if I can get them as well.

I'll keep working on this script, thanks again.  Your suggestion makes sense.  I want to query the user account specified in the file, determine what their home folder path is, then go delete the home folder.

 

Top 10 Contributor
Posts 640

I co-authored the TFM and did SAPIEN's videos. They're just older titles; the TFM isn't really a tutorial - it's more of a reference. It'll be more useful to you after the Lunches book, I suspect.

The newer videos I've done are - well, newer, and HD and awesome, but also - part of a bigger library. For example, the first part follows the Lunches book outline, so it's basically like taking a live class. Then there's a set that gets into scripting and tool making, so it'll grow a bit more. And again, they're more tutorial in nature, with labs and whatnot.

Top 25 Contributor
Posts 123

Thanks, I am looking right now, I'm going to order the book, and I'm trying to find out if I can get the videos covered by work or not, hopefully I'll be able to order them as well.

For the script, I should modify what I found online and then add in the pieces you suggested, then work on modifying it to find the folder paths, is that logical?

 

Top 10 Contributor
Posts 640

Whichever makes more sense for YOU. You're the one who has to write it ;).

Page 1 of 1 (10 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.