One of the very popular and useful tricks with Microsoft Windows is changing the dates and times of your files using Command Prompt (CMD) or PowerShell. This skill is super useful whether you’re an IT pro, someone who manages computer systems, or just someone who loves to organize their files. It helps with sorting files, syncing them up, or even fixing problems.
In this article, we’ll look at how to play around with the dates and timestamps of files using CMD and PowerShell in Windows 11 or Windows 10. Through this guide, you’ll learn how to update these details for both single and multiple files easily.
Page Contents
What are a file’s date and timestamp attributes?
Let’s first understand what the different dates and times are that you might want to change for a file. In Windows, these are the big three:
- The date when the file first came into being.
- The date when the file last got an update or save.
- The date when someone last opened or peeked at the file.
These bits of info are stored with your files and can be 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 basic tool in Windows that lets you talk to your computer in a more direct way. You can use CMD for lots of tasks, including messing with file dates and times.
Using the NirCmd command line
Sadly, CMD by itself doesn’t let you change file dates and times directly. But, no worries! You can grab tools like NirCmd or BulkFileChanger to get the job done. Here’s how you can use NirCmd:
- Grab NirCmd from the NirSoft website.
- Unzip it and make sure your computer knows where to find it by updating your system’s PATH.
- Open up CMD as the boss (administrator).
- Go to the folder with the file you want to tweak.
- Use the
nircmd
command withsetfiletime
, add your file’s name, and slap on the dates and times you want. Like this:
nircmd setfiletime "example.txt" "15-03-2023 10:22:30" "15-03-2023 10:22:30" "15-03-2023 10:22:30"
This command updates the creation, last modified, and last accessed dates and times for example.txt
to March 15, 2023, at 10:22:30 AM. Remember to stick to the “dd-mm-yyyy hh:mm:ss” format.
If you run into trouble changing a file because of permissions, check out: How to Take Ownership of a File, Folder or Drive in Windows 11.
NirCmd also has a neat “now” trick. If you use “now” as your date and time, it sets everything to the current moment, which is handy if you don’t want to type out the exact time. Like so:
nircmd setfiletime "example.txt" now now now
Batch change file date & timestamp using the “for /r” loop with NirCmd
The for /r
loop in CMD is a smart way to go through files and folders, even the ones inside other folders. While CMD doesn’t let you change dates and times on its own, combining for /r
with NirCmd can help you update lots of files at once.
Useful tip: How to Batch Rename Files in Windows 11
Basic Syntax of the for /r
Loop
Here’s the basic idea behind using for /r
:
for /r [Path] %i in ([SearchMask]) do [Command] %i
[Path]
: Where to look. If you skip this, it uses the folder you’re in.[SearchMask]
: Helps you pick which files to work on, like*.txt
for text files.[Command] %i
: What you want to do to each file. The%i
is each file it finds.
Modifying file date and timestamp attributes recursively with for /r
Loop and NirCmd
To change dates and times for a bunch of files with for /r
and NirCmd, do this:
- Make sure NirCmd’s folder is in your system’s PATH as mentioned before.
- Open CMD and go to the folder with the files you want to update.
- Run the
for /r
loop with NirCmd to update each file. Here’s an example:
for /r %i in (*) do nircmd setfiletime "%i" "15-03-2023 10:23:38" "15-03-2023 10:23:38"
This will adjust the creation and modification dates and times for all files to March 15, 2023, 10:23:38 AM.
Related issue: Batch (.BAT) Files Not Running in Windows 11/10
How to change file date & timestamp with PowerShell
PowerShell is a step up from the old command-line interface, CMD. It’s got lots of cool tools built right in that let you do all sorts of things, like changing when a file says it was made or last changed (the date and time attributes).
Getting to know the “Set-ItemProperty” cmdlet
The Set-ItemProperty
cmdlet is super handy. It lets you change details about files, including when they say they were made or last updated.
Also see: How to move all files from subfolders to the main folder in Windows
How to use the Set-ItemProperty
cmdlet
Here’s how you use to the Set-ItemProperty
cmdlet:
Set-ItemProperty -Path [Path] -Name [PropertyName] -Value [NewValue]
- For
-Path [Path]
, tell it where the file is. - With
-Name [PropertyName]
, say which detail you’re changing. - And
-Value [NewValue]
, well, that’s the new detail you’re setting.
Changing when a file says it was made or last updated
To change a file’s date and time info with Set-ItemProperty
, do this:
- Pop open a PowerShell window.
- Create a new date and time object with
Get-Date
. Like this:$NewDate = Get-Date -Year 2023 -Month 3 -Day 15 -Hour 10 -Minute 22 -Second 30
This sets up a date and time for March 15, 2023, at 10:22:30 AM.
- Now, use
Set-ItemProperty
to update the file. Like so:Set-ItemProperty -Path "example.txt" -Name CreationTime -Value $NewDate Set-ItemProperty -Path "example.txt" -Name LastWriteTime -Value $NewDate
These steps will set the created and modified dates for example.txt
to March 15, 2023, 10:22:30 AM.
Changing the last accessed date too
You can also update when a file was last opened with the same cmdlet. Just like this:
Set-ItemProperty -Path "example.txt" -Name LastAccessTime -Value $NewDate
Now, example.txt
will show it was last opened on March 15, 2023, 10:22:30 AM.
Additional resource: Figuring out which process is using a file in Windows 11
Batch changing dates & times for multiple files
The ForEach-Object
cmdlet is great for doing the same thing to lots of files at once. You can grab a bunch of files with Get-ChildItem
and then tell ForEach-Object
what to do with each one.
Basic steps for using ForEach-Object
Here’s how you get started:
Get-ChildItem [Path] | ForEach-Object { [ScriptBlock] }
[Path]
is where your files are.- And
[ScriptBlock]
is what you want to do to each file.
Changing dates and times for a bunch of files
Here’s how to update a lot of files all at once:
- Fire up a PowerShell window.
- Make a new date and time object with
Get-Date
, just like before. - Find the files with
Get-ChildItem
, and then letForEach-Object
do its thing. Check it out: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: Type these lines one by one, hitting Shift + Enter to go down a line. Hit Enter only when you’ve typed it all in.
This will change the created and modified dates for all .txt
files in C:\example
to March 15, 2023, 10:22:30 AM.
Changing dates and times in folders and their subfolders
If you’ve got files in folders within folders, add the -Recurse
option to Get-ChildItem
. Like this:
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 }
Now, all .txt
files in C:\example
and its subfolders will have their dates changed to March 15, 2023, 10:22:30 AM.
Some final words
This guide has shown you how to update when files say they were created or last modified using both CMD and PowerShell. While you need extra tools to do it with CMD, using PowerShell is much easier because the needed commands are already built right into it. Knowing how to change these attributes of files should help you with organizing them, syncing stuff up, or figuring out problems when they pop up.