Copy-Item (based on the creationtime property)

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 3 months ago.

  • Author
    Posts
  • #30786
    Profile photo of Stephen Murphy
    Stephen Murphy
    Participant

    Hello Forum,
    I am very new to powershell, I started learning yesterday.
    I have been playing around with the "Copy-Item" cmdlet, and like what I can do with it.

    I wondered if there was a way of copying files, based on their Property "CreationTime" as I can already do with copying spefic file types?

    I have been playing with both the -Filter and -Exclude paramaters but with no luck so far.

    So example.

    copy from C:\testfolder1\ to C:\testfolder2
    but only copy files from 2014.

    I would be greatful for any help you can offer.

    Kind Regards
    Stephen Murphy

  • #30787
    Profile photo of Don Jones
    Don Jones
    Keymaster

    -Filter and -Exclude operate against the filename. You'll need an interim filter.

    Get-ChildItem c:\source |
    Where-Object CreationTime -gt [datetime]'1/1/2001' |
    Copy-Item c:\destination
    

    Something vaguely like that, conceptually.

  • #30791
    Profile photo of Stephen Murphy
    Stephen Murphy
    Participant

    Don, thats lovely thank you, I think I understand a bit better now.
    Can you do the same with -gt "12 months" or "30 days" or does it have to be specific dates?

  • #30793
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, you have to have an actual value. It's just like comparing two numbers. But you can make one.

    $tomorrow = (Get-Date).AddDays(1)

    Pretty sure it's all in "Learn PowerShell in a Month of Lunches" if you want to explore deeper :).

You must be logged in to reply to this topic.