Use TAKEOWN and ICACLS with very long paths and filenames

article #1361, updated 1704 days ago

If there are very long folder or file names, or the path is just too long somewhere down in the tree, this will fail:

ICACLS X:\folder /Q /C /T /reset

but this will succeed:

ICACLS "\\?\X:\folder" /Q /C /T /reset

Apparently the latter incorporates a different API somewhere in the chain.

For TAKEOWN, we just have to run it in Powershell, not CMD.

Powershell code to do it all at once, while CD’d to the level just above, using command-line parameter to specify folder name, is here:

param(
[string]$location
)

$iexcmd = 'TAKEOWN /F ' + '"E:\Shared Data\' + $location + '" /R /D Y'
Write-Progress -Activity "Reset Permissions" -CurrentOperation $iexcmd -PercentComplete -1
Write-Host $iexcmd
iex $iexcmd | Out-Null

$iexcmd = 'ICACLS ' + '"\\?\E:\Shared Data\' + $location + '" /Q /C /T /reset'
Write-Progress -Activity "Reset Permissions" -CurrentOperation $iexcmd -PercentComplete -1
Write-Host $iexcmd
iex $iexcmd | Out-Null

Categories: