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