Delete home folders for user list?

rated by 0 users
This post has 0 Replies | 1 Follower

Top 25 Contributor
Posts 115
JakeS Posted: 04-18-2012 9:08 AM

Hello,

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 {
  $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 

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