PowerShell脚本移动文件和文件夹,包括从一个地方到另一个子比X天以上(PowerShell

2019-08-17 00:43发布

我公司开发的PowerShell脚本,它的工作绝对没问题。 唯一的挑战是在子文件夹中的文件都没有得到移动到目的地。

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

我无法进一步自定义脚本。

Answer 1:

使用-Recurse的选项Get-ChildItem命令打通文件的子文件,然后通过管道收集到移动每个单独Move-Item

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

下面是截图:



Answer 2:

不要浪费你的时间试图重新发明robocopy在PowerShell中。

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


Answer 3:

上述的简化
robocopy A:\ B:\ /MIR /minage:31
其中A:\是您的源B:\是您的目的地



Answer 4:

我需要一个快速班轮要将所有数据从一个驱动器到另一个。 这完全为我工作:

GET-ChildItem “E:” -Recurse | 布展项目-Destination“G:”



文章来源: PowerShell script to move files and folders including subfolders from one location to another older than x days