Monday, March 31, 2014

Powershell - File Date Change Routine

There are times you need to have files that are created and modified on a specific date for integration testing some file management or metadata gathering routine. This is a simple routine to assist in that.

$varFileName=read-host "Enter path and file name e.g. c:\dir\file.zip"
$varCreatedDateOffset=read-host "Enter the created date offset in days e.g. -7"
$varModifiedDateOffset=read-host "Enter the modified date offset in days e.g. 0"

    $change = Get-Item $varFileName
    $createDate = Get-Date
    $modDate = Get-Date
    $createDate = $createDate.AddDays($varCreatedDateOffset)
    $modDate = $modDate.AddDays($varModifiedDateOffset)
    $change.CreationTime = $createDate
    $change.LastWriteTime = $modDate
    Get-Item $change|Select-Object Mode,CreationTime,LastWriteTime,Length,Name

No comments:

Post a Comment