How to Get File Creation Date in PowerShell?

When managing files on a Windows system, you might often need to know when a file was created. In this PowerShell tutorial, we’ll explore various methods to get the file creation date using PowerShell with various examples.

To get the file creation date in PowerShell, you can use the Get-Item cmdlet followed by accessing the CreationTime property of the resulting object. For example:

$file = Get-Item "C:\MyFolder\MyFile.txt"
$creationDate = $file.CreationTime
Write-Output "The file was created on: $creationDate"

This script will output the exact date and time when the file was created.

Get File Creation Date in PowerShell

PowerShell provides various cmdlets to get the created date of a file. Let us explorer all these methods:

Method 1: Using Get-Item

The Get-Item cmdlet is the widely used PowerShell command for retrieving a file’s creation date. It retrieves the item at a specified location and returns an object that represents the item, including its properties.

Here’s a simple example:

$file = Get-Item "C:\MyFolder\Notes.docx"
$creationDate = $file.CreationTime
Write-Output "The file was created on: $creationDate"

This script will output the creation date and time of the specified file. The CreationTime property of the file object contains the information we need.

I executed the PowerShell script, and you can see the output in the screenshot below:

Get File Creation Date in PowerShell

Method 2: Using Get-ChildItem

If you are working with multiple files, then you can use the PowerShell Get-ChildItem cmdlet.

Here is an example to get the file creation date of all the files presented in a directory using PowerShell.

$files = Get-ChildItem "C:\MyFolder\*"
foreach ($file in $files) {
    $creationDate = $file.CreationTime
    Write-Output "The file $($file.Name) was created on: $creationDate"
}

This script lists all the files in a directory and prints their creation dates. It’s particularly useful for inventorying multiple files.

You can see the output below, after I executed the above PowerShell script.

How to Get File Creation Date in PowerShell

Method 3: Using the [System.IO.File] Class

If you want to use the .Net classes in PowerShell, then you can use the [System.IO.File] class to access the creation date of a file.

$filePath = "C:\MyFolder\Notes.docx"
$creationDate = [System.IO.File]::GetCreationTime($filePath)
Write-Output "The file was created on: $creationDate"

This method directly taps into the .NET framework’s file handling capabilities, which can be more familiar to those with a background in .NET languages.

Method 4: Using WMI Objects

Windows Management Instrumentation (WMI) is another powerful feature that can be utilized with PowerShell to get file creation dates. Here’s how you can do it:

$filePath = "C:\MyFolder\file.txt"
$creationDate = Get-WmiObject -Query "SELECT CreationDate FROM CIM_DataFile WHERE Name = '$filePath'"
Write-Output "The file was created on: $($creationDate.ConvertToDateTime($creationDate.CreationDate))"

This script uses WMI to query the CIM_DataFile class, which represents a data file on a computer system running Windows. Note that you need to replace the backslashes in the file path with double backslashes in the WMI query.

Method 5: Using PowerShell Custom Functions

You can also create a custom PowerShell function to encapsulate the logic for retrieving file creation dates and make it reusable.

function Get-FileCreationDate {
    param (
        [string]$filePath
    )
    $file = Get-Item $filePath
    return $file.CreationTime
}

# Usage
$creationDate = Get-FileCreationDate -filePath "C:\MyFolder\file.txt"
Write-Output "The file was created on: $creationDate"

With this function, you can easily call Get-FileCreationDate with different file paths to get their creation dates.

Conclusion

PowerShell offers multiple ways to retrieve the creation date of a file. In this PowerShell tutorial, I have explained how to get file creation date in PowerShell using various methods and I hope it will help you.

You may also like: