Delete home folders for user list?

rated by 0 users
Not Answered This post has 0 verified answers | 0 Replies | 1 Follower

Top 50 Contributor
158 Posts
JakeS posted on 04-18-2012 9:08 AM


I am trying to find a way to modify this script so that it will accept a list of usernames and then delete the home folders for the users.  The home folders exist on numerous file servers and I can put the user names in a text or csv file.

Can anyone help me out and thanks in advance!

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

Invoke-Command -Session $PSSession -ScriptBlock {
  $Filter = "(&(ObjectCategory=user)(Name=helpme testuser))"
  $Searcher = [adsiSearcher]($Filter)
  $Searcher.Findall() | 
    ForEach-Object { 
      $HD = $ 
      $PD = $
      $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 

All Replies

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