One of the lesser-known but incredibly useful features of Microsoft Windows is the ability to manipulate file date and timestamp attributes using the Command Prompt (CMD) and PowerShell. Whether you’re an IT professional, a system administrator, or simply a power user, knowing how to change a file’s metadata using these built-in tools can be a lifesaver when it comes to file organization, synchronization, or even troubleshooting.
In this article, we’ll dive into the world of file date and timestamp manipulation, exploring various techniques and commands to modify these attributes for a single file or multiple files in batch using CMD and PowerShell in Windows 11 or Windows 10. By the end of this guide, you’ll have a solid understanding of the processes involved, and you’ll be able to change the date and timestamp attributes for a single file or multiple files like a pro.
Page Contents
Understanding file date and timestamp attributes
Before we dive into the techniques, it’s essential to understand the various file date and timestamp attributes that you might need to manipulate. In the context of Windows, there are three primary attributes:
- Creation Date: The date when the file was first created
- Last Modified Date: The date when the file was last altered or saved
- Last Accessed Date: The date when the file was last opened or read
These attributes are stored as metadata within the file system and can be viewed and modified as needed.
See also: How to Change Date and Time Format in Windows 11
Changing file date & timestamp via CMD
The Command Prompt (CMD) is a built-in Windows tool that provides a command-line interface for interacting with the operating system. You can use CMD to perform various tasks, including changing file date and timestamp attributes.
Using the NirCmd command line
Unfortunately, CMD does not have a built-in command that directly allows you to change file date and timestamp attributes. However, you can use third-party windows command line applications, such as NirCmd or BulkFileChanger, to accomplish this task. For example, you can use NirCmd as follows:
- Download NirCmd from the NirSoft website.
- Extract the contents to a folder, and add the folder’s path to your system’s PATH environment variable.
- Launch CMD with administrator.
- Navigate to the directory containing the file you want to modify.
- Use the
nircmd
command followed by thesetfiletime
parameter, the target file, and the desired date and time values. For example:
nircmd setfiletime "example.txt" "15-03-2023 10:22:30" "15-03-2023 10:22:30" "15-03-2023 10:22:30"
This command will change the creation, last modification and last access date and time (in that order) of example.txt
to March 15, 2023, 10:22:30 AM. You must use the same date and time format: “dd-mm-yyyy hh:mm:ss“.
If you have issue modifying the date and time attributes of a file due to permission error, read: How to Take Ownership of a File, Folder or Drive in Windows 11.
In addition to specifying a specific date and time, NirCmd also allows you to use the “now” parameter. When you use “now” as an argument, it will set the file’s creation, last modification, or last access date and time to the current date and time on your system. This is particularly useful if you want to update one or more of these attributes to the present moment without manually inputting the current date and time. For example:
nircmd setfiletime "example.txt" now now now
Batch change file date & timestamp using the “for /r” loop with NirCmd
The for /r
loop is a powerful construct in CMD that allows you to iterate through files and directories, including those within subdirectories. While CMD does not have a built-in command to change file date and timestamp attributes, you can use the for /r
loop with NirCmd to modify these attributes for multiple files.
Useful tip: How to Batch Rename Files in Windows 11
Basic Syntax of the for /r
Loop
The basic syntax of the for /r
loop is as follows:
for /r [Path] %i in ([SearchMask]) do [Command] %i
[Path]
: Specifies the path to the folder where the search will be performed. If not provided, it defaults to the current directory.[SearchMask]
: Specifies the search mask, such as*.txt
, to filter the files on which the command will be executed.[Command] %i
: The command to execute for each file that matches the search mask. The%i
variable represents the current file in the loop.
Modifying file date and timestamp attributes recursively with for /r
Loop and NirCmd
To use the for /r
loop with NirCmd to change file date and timestamp attributes, follow these steps:
- Ensure that you have added the NirCmd folder’s path to your system’s PATH environment variable, as previously explained.
- Open a Command Prompt window and navigate to the directory containing the files you want to modify.
- Use the
for /r
loop with the search mask to filter the desired files and execute the NirCmdsetfiletime
command for each file. For example:
for /r %i in (*) do nircmd setfiletime "%i" "15-03-2023 10:23:38" "15-03-2023 10:23:38"
This command will change the creation and modification date and time of all .txt
files in the current directory and all its subdirectories to March 15, 2023, 10:22:30 AM.
Related issue: Batch (.BAT) Files Not Running in Windows 11/10
By understanding and leveraging the for /r
loop in conjunction with NirCmd, you can easily modify file date and timestamp attributes for multiple files at once, streamlining your file management tasks.
Changing file date & timestamp via PowerShell
PowerShell is a more advanced and powerful command-line interface compared to CMD, providing a wide range of built-in cmdlets that can be used to perform various tasks, including changing file date and time attributes.
Using the “Set-ItemProperty” cmdlet
The Set-ItemProperty
cmdlet is a versatile PowerShell cmdlet that allows you to modify the properties of items, including files and their date and timestamp attributes.
Also see: Move All Files from Subfolders to Main Folder (Windows)
Basic syntax of the Set-ItemProperty
cmdlet
The basic syntax of the Set-ItemProperty
cmdlet is as follows:
Set-ItemProperty -Path [Path] -Name [PropertyName] -Value [NewValue]
-Path [Path]
: Specifies the path to the file or item whose property you want to modify.-Name [PropertyName]
: Specifies the name of the property you want to change.-Value [NewValue]
: Specifies the new value for the property.
Modifying file date and timestamp attributes with Set-ItemProperty
To use the Set-ItemProperty
cmdlet to change file date and timestamp attributes, follow these steps:
- Open a PowerShell window.
- Use the
Get-Date
cmdlet to create a new DateTime object with the desired date and time. For example:$NewDate = Get-Date -Year 2023 -Month 3 -Day 15 -Hour 10 -Minute 22 -Second 30
This command will create a new DateTime object representing March 15, 2023, 10:22:30 AM.
- Use the
Set-ItemProperty
cmdlet to change the file date and timestamp attributes. For example:Set-ItemProperty -Path "example.txt" -Name CreationTime -Value $NewDate Set-ItemProperty -Path "example.txt" -Name LastWriteTime -Value $NewDate
These commands will change the creation and modification date and time of example.txt
to March 15, 2023, 10:22:30 AM.
Changing last accessed date with Set-ItemProperty
In addition to modifying the creation and modification date and time, you can also change the last accessed date and time using the Set-ItemProperty
cmdlet. For example:
Set-ItemProperty -Path "example.txt" -Name LastAccessTime -Value $NewDate
This command will change the last accessed date and time of example.txt
to March 15, 2023, 10:22:30 AM.
Additional resource: How to Know Which Process is Using a File in Windows 11
Using the “ForEach-Object” cmdlet to batch change the date & time for multiple files
The ForEach-Object
cmdlet, also known as the %
alias, is a versatile PowerShell cmdlet that allows you to perform an operation on each item within a collection, such as files returned by the Get-ChildItem
cmdlet.
Basic syntax of the ForEach-Object
cmdlet
The basic syntax of the ForEach-Object
cmdlet is as follows:
Get-ChildItem [Path] | ForEach-Object { [ScriptBlock] }
[Path]
: Specifies the path to the folder containing the files you want to process.[ScriptBlock]
: A script block containing the operations to be performed on each item.
Modifying file date and timestamp attributes with ForEach-Object
To use the ForEach-Object
cmdlet to batch change the date and timestamp attributes of multiple files, follow these steps:
- Open a PowerShell window.
- Use the
Get-Date
cmdlet to create a new DateTime object with the desired date and time. For example:$NewDate = Get-Date -Year 2023 -Month 3 -Day 15 -Hour 10 -Minute 22 -Second 30
This command will create a new DateTime object representing March 15, 2023, 10:22:30 AM.
- Use the
Get-ChildItem
cmdlet to retrieve the files you want to modify, and then pipe the output to theForEach-Object
cmdlet. In the script block, use theSet-ItemProperty
cmdlet to change the file date and timestamp attributes. For example:Get-ChildItem -Path "C:\example\*.txt" | ForEach-Object { Set-ItemProperty -Path $_.FullName -Name CreationTime -Value $NewDate Set-ItemProperty -Path $_.FullName -Name LastWriteTime -Value $NewDate }
Note: To ensure that this script block works correctly in PowerShell, enter each line one at a time, using Shift + Enter to move to the next line. Press Enter to execute the command only after the entire script block has been entered.
This script will change the creation and modification date and time of all .txt
files in the C:\example
directory to March 15, 2023, 10:22:30 AM.
Modifying file date and timestamp attributes recursively with ForEach-Object
If you want to batch change the file date and timestamp attributes for files within subdirectories as well, you can use the -Recurse
parameter with the Get-ChildItem
cmdlet. Here’s an example:
Get-ChildItem -Path "C:\example\*.txt" -Recurse | ForEach-Object { Set-ItemProperty -Path $_.FullName -Name CreationTime -Value $NewDate Set-ItemProperty -Path $_.FullName -Name LastWriteTime -Value $NewDate }
This script will change the creation and modification date and time of all .txt
files in the C:\example
directory and all its subdirectories to March 15, 2023, 10:22:30 AM.
By understanding and leveraging the ForEach-Object
cmdlet in PowerShell, you can efficiently modify file date and timestamp attributes for multiple files at once, greatly simplifying your file management tasks.
Wrap-up
In this comprehensive guide, we’ve explored various techniques for changing file date and timestamp attributes using both CMD and PowerShell. While CMD requires the use of third-party tools like NirCmd for this task, PowerShell offers a more streamlined and powerful approach with its built-in cmdlets. By understanding these commands and techniques, you’ll be well-equipped to manage file metadata effectively and efficiently for your specific needs.
Now that you know how to change file date and timestamp attributes via CMD or PowerShell, you can harness this powerful capability to maintain better control over your files, ensure proper synchronization, and troubleshoot issues when needed.