Home > Mobile >  I am on Powershell 5.1 I am looping over a directory and I need to 275 days to the CreationTime and
I am on Powershell 5.1 I am looping over a directory and I need to 275 days to the CreationTime and

Time:03-05

I need to add 275 days to the Each file in the directories $object.CreationTime and $object.LastWriteTime. In the same format as this 10 November 2016 12:00:00. I will take that variable and remove the strings being passed in now.

$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")
}

CodePudding user response:

If you want to "add 275 days" to the timestamps, use the AddDays method. The question makes it appear that you want to hardcode a specific date.

$files =  Get-ChildItem -force | Where-Object {! $_.PSIsContainer} 
foreach($object in $files)
{
     $object.CreationTime = $object.CreationTime.AddDays(275)
     $object.LastWriteTime = $object.LastWriteTime.AddDays(275)
}

Using PowerShell 5.1 and higher, the -File switch obviates the need to check PSIsContainer.

$files = Get-ChildItem -Force -File

CodePudding user response:

If I understand correctly this may work...

$newDateString = '10 November 2016 12:00:00';

In loop:

$object.CreationTime = Get-Date($newDateString);
$object.LastWriteTime = Get-Date($newDateString);
  • Related