Very often System administrator has to work with file and folder operations to get file creation date timestamp, and filename to extract all information in CSV based on filter criteria.
I love working with PowerShell scripts as tasks easily get done in no time. Using PowerShell to get file creation date format, the filename can be easily retrieved.
To get the file creation date in PowerShell, use the Get-ChildItem
cmdlet. The Get-ChildItem is used to get items or child items in one or more specified locations. This command uses the CreationTime
property to get the file creation date.
In this article, we will discuss how to get file creation date, get files created today using PowerShell.
PowerShell Get File Creation TimeStamp
To get the file date timestamp in PowerShell, use the Get-ChildItem cmdlet. Run the below command.
Get-ChildItem -Path D:\PowerShell\ActiveDirectoryGroupList.csv | select Name,CreationTime
In the above PowerShell script, the Get-ChildItem
gets a file object using a specified location as specified in the -Path parameter and passes the output to the Select cmdlet.
The Select command selects the file Name
and the file CreationTime properties and prints it on the PowerShell console as below.
Name CreationTime
---- ------------
ActiveDirectoryGroupList.csv 04-10-2020 14:40:07
Cool Tip: How to use Get-Date to get current date time in PowerShell!
PowerShell Get Files Created Today
To get files created today using PowerShell, use the Get-ChildItem cmdlet to get files and compare the file creation date with today’s date. Run the below command.
Get-ChildItem -File | Where-Object {$_.CreationTime -gt (Get-Date).Date}
In the above PowerShell script to get files created today, the Get-ChildItem command gets the file object in the current directory and passes the output to the Where-Object cmdlet.
The Where-Object command compares file CreationTime greater than today’s date (Get-Date) to get files created today and prints output on the console.
Directory: D:\PowerShell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 24-07-2021 20:40 46 GetFileProperties-Assignment.txt
PowerShell – Get all Files with the Creation Date
To get all files with creation date-time in PowerShell in descending order as per creation date timestamp, use the below command.
Get-ChildItem -File | select Name,CreationTime | sort CreationTime -descending
In the above PowerShell command, the Get-ChildItem
command uses the File
parameter to get file objects in the current directory and pass output to the Select command.
The Select command selects the file name and file creation time and passes the output to the Sort command.
The Sort command sorts file objects descending by their creation time to get the latest file created on top of the list and prints the list of files on console as below.
Name CreationTime
---- ------------
GetFileProperties-Assignment.txt 24-07-2021 20:39:42
print-spooler-service.ps1 03-07-2021 17:01:55
comppingstatus.csv 24-10-2020 20:03:35
complist.txt 24-10-2020 19:15:39
vscode-docker-master.zip 06-10-2020 18:08:39
ActiveDirectoryGroupList.csv 04-10-2020 14:40:07
Employee.xlsx 04-10-2020 12:24:42
PowerShell Get File Creation Date by extension
To find files creation date by file extension, use PowerShell Get-ChildItem cmdlet to get file object and apply extension filter to select file only.
Get-ChildItem -File -Recurse| Where {$_.Extension -eq ".txt"} | select Name,CreationTime
The above PowerShell Get-ChildItem command gets file objects recursively through folders and subfolders and passes the output to the Where
command.
The Where
command search for file extension equal to .txt and gets only *.txt file objects and passes output to the Select
command.
The Select command selects file name and file creation date time and prints it on the console.
Cool Tip: How to find large size files in PowerShell!
Get Files Count Created Today
To get files count created today, run below PowerShell command
(Get-ChildItem -File | Where-Object {$_.CreationTime -gt (Get-Date).Date} | Measure-Object).Count
In the above PowerShell script, the Get-ChildItem cmdlet gets the file objects and passes them to the Where-Object
command.
The Where-Object
command compares the file creation date time with today’s date and returns file objects to the Measure-Object
command.
The Measure-Object
command performs the calculation over file objects and gets file count created today.
Cool Tip: How to delete file if exists in PowerShell!
Conclusion
I hope the above article guides you to effectively use the PowerShell Get-ChildItem cmdlet to get file creation date or find files created today.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.