Monday 19 November 2012

Take ownership and change permissions from the Windows 7 command line

I discovered a unique 'feature' of Windows 7 offline files today; It turns out that when offline files are created they are stripped of ownership and all permissions.

This little gem of knowledge I discovered when I had a client who made edits to an offline file, during which time the offline file service was stopped in the domain the computer was part of. When the laptop was returned to the domain the offline files and syncronisation were disabled, rendering all changes lost. Or were they?

I tracked down the offline files, hidden away in %systemroot%\CSC, and copied them all to a safe location on the desktop so that they could be opened and saved to the network again. However, this is when I made my discovery! I was unable to open any of the folders or files as the user account or the administrator account. I was also unable to request permission or change permissions.

After some scrutiny I discovered that I was able to retake ownership of files and begin my trek through the maze of folders and files in a very large offline files collection. Unfortunately by using explorer I had to take control and give myself permission to open every folder in the directory tree individually. This was going to be a long process and a major headache. So I went hunting in Googleland.

During my searches I found a wonderful command line tool built into Windows 7 for taking ownership and granting permissions on a whole directory. The first command allows the logged on user to take ownership of all the files and folders:

takeown /f directory_name /r /d y

The second command allows you to grant full permissions to any user account to all of the files and folders in the directory:

icacls directory_name /grant administrators:F /t

These commands were life savers for me and I give credit to eset_boy on
The Windows Club Forum for his wonderful advice.