Advertisement
jdkaufman

Reclaim-AdminOwnership

Jul 28th, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $path = Read-Host 'What is the path of the User folders? (Example D:\e)'
  2. $min = Read-Host 'What is min value of User folder to process (Example A00* - use a wildcard)'
  3. $max = Read-Host 'What is max value of User folder to process (Example A99* - use a wildcard)'
  4. $mainDir = "$path"
  5. $logFile = "$path\Resetpermissions$min$max.log"
  6. $dirs = gci "$mainDir" |? {$_.psiscontainer} | where {($_.Name -le "$max" -and $_.Name -ge "$min")}
  7. Get-Date | Add-Content $LogFile
  8. write "Changing permissions on" $path
  9. foreach ($dir in $dirs){
  10. write-output $dir.fullname | Add-Content $LogFile
  11. write "Have Administrator take ownership of file" | Add-Content $LogFile
  12. takeown.exe /F $($dir.fullname) /R /D Y | Add-Content $LogFile
  13. write "Reset permissions on all folders under path" | Add-Content $LogFile
  14. icacls.exe $($dir.fullname) /reset /T /C /L | Add-Content $LogFile
  15. write "Grant permission to User where User=directoryname" | Add-Content $Logfile
  16. icacls.exe $($dir.fullname) /grant ($($dir.basename) + ':(OI)(CI)F') /C /L | Add-Content $LogFile
  17. write "Set owner to user" | Add-Content $LogFile
  18. icacls.exe $($dir.fullname) /setowner $($dir.basename) /T /C /L | Add-Content $Logfile
  19. }
  20. Get-Date | Add-Content $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement