I have roaming profiles which I need to delete. By default these are created automatically when a user logs in and the user is the only one who can access them without manually taking ownership. I have lots of these, regularly. The below code is what I'm using, but it does not propagate to all folders under the root on one pass. It will make me owner of the root folder on the first run, then individual folders on the second run and so on until I am finally owner of everything, might take 8-10 runs before I can actually delete the root user folder.
When manually taking ownership, there is a check box "Replace owner on subcontainers and objects", how do I do this with powershell.
Here is the code I've been using:
takeown /f $folder /R /D y
ICACLS $Folder /grant "$user:(CI)F" /T
Remove-Item $folder -Recurse -Force