Home > Blockchain >  PowerShell set random Creation Date
PowerShell set random Creation Date

Time:08-06

how can i create different random creation-dates for files in a Folder? random days, months, years and random time like Date between 01.01.1980 - 05.08.2022

    $files =  Get-ChildItem -force | Where-Object {! $_.PSIsContainer} 
foreach($object in $files)
{
     $object.CreationTime=("10 November 2016 12:00:00")
     $object.LastWriteTime=("10 November 2016 12:00:00")
     $object.LastAccessTime=("10 November 2016 12:00:00")
}

CodePudding user response:

You can use Get-Date to create a variable with the proper [DateTime] type. And you can use Get-Random to create random numbers in the desired range.

$files =  Get-ChildItem -Force -File
$Start = Get-Date '01.01.1980'
$End = Get-Date '05.08.2022'
foreach($object in $files){
    $Random = Get-Random -Minimum $Start.Ticks -Maximum $End.Ticks
    $object.CreationTime = [datetime]$Random
    $object.LastWriteTime = [datetime]$Random
    $object.LastAccessTime = [datetime]$Random
}
  • Related