Move Files older then 31 days to another drive

前端 未结 2 1430
北恋
北恋 2021-02-05 16:56
Function Move {
  #Moves all files older than 31 days old from the Source folder to the Target 
  Get-Childitem -Path \"E:\\source\" | Where-Object { $_.LastWriteTime -l         


        
相关标签:
2条回答
  • 2021-02-05 17:18

    I don't know if this makes much of a difference, but rather than $. it needs to be $_.

    I tried this script and it works fine for me:

    get-childitem -Path "E:\source" |
        where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | 
        move-item -destination "F:\target"
    

    Notice you don't need a foreach loop because the objects will be "piped" into the move-item command

    0 讨论(0)
  • 2021-02-05 17:36

    Also be aware of hidden files, try adding -Force to Get-ChildItem

    0 讨论(0)
提交回复
热议问题