Windows 10, Windows 11, Windows PE, Windows Server

Fix NTFS Permissions and Migrate Files with ICACLS and RoboCopy



📁 Handling Corrupted Permissions and Secure File Transfers

As a system administrator, you’ll inevitably encounter directories that are inaccessible, corrupted, or resistant to modification or deletion due to broken or misconfigured permissions. There are also times when you need to apply a specific set of NTFS permissions to a directory, or transfer data between drives while preserving file timestamps and security attributes. In these situations, ICACLS and RoboCopy are indispensable tools for restoring order and ensuring secure, consistent file operations.

🔐 Restoring NTFS Permissions with ICACLS and Creating Permission Templates

If a directory’s permissions are corrupted or missing, you can reset them by reapplying default or backed-up permissions:

BAT (Batchfile)
icacls "C:\Path\To\CorruptedDir" /reset /T /C /Q

/reset: Replaces ACLs with default inherited permissions.

/T: Applies to all subfolders and files.

/C: Continues on errors.

/Q: Quiet mode.

📄 Template: Backup and Apply NTFS Permissions from One Folder to Another

Step 1: Save (Export) Permissions from the Source Folder

BAT (Batchfile)
icacls "C:\Path\To\SourceDir" /save "C:\perms_backup\source_perms.acl" /T

Step 2: Apply Saved Permissions to Target Folder

BAT (Batchfile)
icacls "C:\Path\To\TargetDir" /restore "C:\perms_backup\source_perms.acl"

This will replicate the exact permission structure from SourceDir to TargetDir, including subdirectories.

💾 Copy Drive Contents with RoboCopy — Preserve Timestamps, NTFS Permissions, and Log Everything

To copy all contents from one drive to another while preserving timestamps, NTFS permissions, and skipping retries, use the following RoboCopy command:

BAT (Batchfile)
robocopy D:\ E:\ /MIR /COPYALL /DCOPY:T /R:0 /W:0 /LOG:"C:\Logs\robocopy_log.txt"

🔍 Explanation of switches:

  • D:\ → Source drive
  • E:\ → Destination drive
  • /MIR → Mirrors directory tree (equivalent to /E and /PURGE)
  • /COPYALL → Copies all file info: data, attributes, timestamps, NTFS ACLs, and owner info
  • /DCOPY:T → Preserves directory timestamps
  • /R:0 → Sets 0 retries on failed copies
  • /W:0 → Sets 0 wait time between retries
  • /LOG:"C:\Logs\robocopy_log.txt" → Outputs progress and errors to a log file

Tip: Always test with /L (list only) before running full copy to preview what RoboCopy would do:

BAT (Batchfile)
robocopy D:\ E:\ /MIR /COPYALL /DCOPY:T /R:0 /W:0 /L

🧊 Copy Locked Files with RoboCopy via VSS Shadow Copy

🔧 Step-by-Step Instructions:

Step 1: Create a Shadow Copy Using DiskShadow

Create a .ds script file (e.g., shadow.ds) with the following contents:

BAT (Batchfile)
SET CONTEXT PERSISTENT
SET METADATA C:\Shadow\meta.cab
BEGIN BACKUP
ADD VOLUME D: ALIAS MyShadow
CREATE
EXPOSE %MyShadow% X:

This script:

  • Creates a persistent shadow copy of drive D:
  • Mounts the snapshot as drive X: (you can change this to any unused letter)

Run the script with:

BAT (Batchfile)
diskshadow /s C:\Shadow\shadow.ds

✅ After running, you can access D: as it was at snapshot time via X:\

Step 3: Clean Up

When done, remove the exposed drive and delete the shadow copy:

BAT (Batchfile)
diskshadow

Inside DiskShadow prompt:

BAT (Batchfile)
DELETE SHADOWS ALL

Or use vssadmin delete shadows for more control:

BAT (Batchfile)
vssadmin delete shadows /for=D: /all

🛑 Notes:

  • Requires administrative privileges
  • Make sure no critical app writes to the destination during the copy
  • This approach ensures consistent copies even of in-use files (e.g., databases, logs)

Let me know if you want a full script that automates all steps!

✅ Conclusion

By combining ICACLS and RoboCopy, you gain powerful control over NTFS permissions and reliable data transfers—even in complex scenarios like restoring corrupted ACLs or copying locked files. Whether you’re backing up critical systems or migrating data securely, these tools offer the precision and flexibility Windows administrators need. With proper use of permission templates, logging, and VSS snapshots, your file operations can be both safe and efficient.

🌿 Final Thoughts

ICACLS and RoboCopy are essential tools in any Windows admin’s toolkit. With the right commands and a bit of planning, you can handle permissions, backups, and file migrations with confidence.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *