Monday, December 09, 2024

Windows Powershell command to touch all my files in a directory

 Credit to chatGPT:

In Windows 11, you can use a simple PowerShell command to "touch" all your files, updating their timestamps so OneDrive considers them "fresh" without making actual content changes. Here's how you can do it:

Steps to Update File Timestamps

  1. Open PowerShell as Administrator:

    • Press Win + X and select Windows Terminal (Admin) or PowerShell (Admin).
  2. Navigate to the folder you want to "touch":

    powershell
    cd "C:\Path\To\Your\Folder"
  3. Run the following command to update the LastWriteTime for all files recursively:

    powershell
    Get-ChildItem -Recurse | ForEach-Object { $_.LastWriteTime = Get-Date }

    This will set the LastWriteTime of all files to the current date and time.


Explanation:

  • Get-ChildItem: Retrieves all files and directories in the folder.
  • -Recurse: Processes all subfolders and their contents.
  • ForEach-Object: Iterates through each item.
  • LastWriteTime: Updates the timestamp to Get-Date (current date and time).

Important Notes:

  1. Test First: Run this in a small test folder to ensure it behaves as expected.
  2. OneDrive Sync: After modifying timestamps, OneDrive should sync the updated metadata and treat the files as fresh.
  3. Preserve Original Timestamps: If you want to preserve original timestamps, back them up before running this script.

Let me know if you need further assistance!