0

I have created the below script, which has been tested and working successfully, to amend the 'Last Modified Date' of all files contained in the selected folder.

$a = Get-Date "22/11/2012 10:00 AM"

$b = Get-ChildItem "C:\MyFiles"

foreach ($i in $b)
{
    $i.LastWriteTime = $a
    $a = $a.AddMinutes(1)    
}

$b

I am just looking for some help amending this script to include all subfolders/file within the selected folder, as I am currently having to amend this manually to change the date within the sub-folders of "C:\MyFiles".. for e.g. "C:\MyFiles\A", "C:\MyFiles\B".. etc.

In addition.. I was also wondering how I can remove the line "$a = Get-Date "22/11/2012 10:00 AM" so that it automatically sets the date to todays, and I do not have to enter a date manually.

1
  • I am very new to Powershell but.. I have now amended this to get the current date/time, but I assume that $a.AddMinutes can now be removed/amended as this would not be required? $a = Get-Date $b = Get-ChildItem "C:\MyFiles" foreach ($i in $b) { $i.LastWriteTime = $a $a = $a.AddMinutes(0) } $b Commented Nov 22, 2012 at 12:09

1 Answer 1

1

like this?

    $a = get-date
    $b = Get-ChildItem "C:\MyFiles" -recurse | ? { !$_.psiscontainer }
    foreach ($i in $b)
    {
        $i.LastWriteTime = $a 
        $a = $a.AddMinutes(1)    
    }

    $b

or if you don't need to add a minute after each file:

     $dir = read-host "Insert path"
     $b = Get-ChildItem $dir -recurse | ? { !$_.psiscontainer }
    foreach ($i in $b)
    {
        $i.LastWriteTime = get-date               
    }

    $b
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect!.. this now amends the time/date within all sub-folders!. I don't suppose you would mind helping with above comment, is the $a.AddMinutes needed? as I don't really need to have the date showing as +1 minute of that of its previous file, these can all just be set to todays date/time.
Cheer!.. this is going a bit further than what I need, but how would I change the selected directory "C:\MyFiles" into actual input, therefore allowing me to just enter the directory I require before hand, as that is the only bit now I am having to amend manually within the script (.ps1) itself.
@welowkey Edited my aswer. Try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.