PowerShell script to move files and folders including subfolders from one location to another older than x days

痞子三分冷 提交于 2019-12-05 12:03:59

问题


I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination.

get-childitem -Path "\\servername\location" |
    where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
    move-item -destination "C:\Dumps"

I am unable to customize the script further.


回答1:


Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the collection to Move-Item

Get-ChildItem -Path "C:\Test" -Recurse |
  Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-31)} |
  Move-Item -destination "C:\Dumps"

Here's a screenshot:




回答2:


Don't waste your time trying to re-invent robocopy in PowerShell.

robocopy \\servername\location C:\Dumps /e /mov /minage:31



回答3:


Simplification of the above
robocopy A:\ B:\ /MIR /minage:31
Where A:\ is your source B:\ is your destination




回答4:


I needed a quick one liner to move all data off one drive onto another. This worked perfectly for me:

Get-ChildItem "E:" -Recurse | Move-Item -Destination "G:"



来源:https://stackoverflow.com/questions/14935076/powershell-script-to-move-files-and-folders-including-subfolders-from-one-locati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!