PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置

时间:2013-02-18 11:20:37

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v1.0

我开发了一个PowerShell脚本,它的工作非常好。唯一的挑战是子文件夹中的文件没有移动到目的地。

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

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

4 个答案:

答案 0 :(得分:16)

不要浪费时间尝试在PowerShell中重新发明robocopy

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

答案 1 :(得分:11)

使用-Recurse命令上的Get-ChildItem选项来查看子文件夹中的文件,然后通过将集合管道传输到Move-Item

来单独移动每个文件
Get-ChildItem -Path "C:\Test" -Recurse |
  Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-31)} |
  Move-Item -destination "C:\Dumps"

以下是截图:

screenshot

答案 2 :(得分:7)

简化上述内容
robocopy A:\ B:\ /MIR /minage:31
哪里 答:\是你的来源 B:\是你的目的地

答案 3 :(得分:0)

我需要一个快速的衬垫,将所有数据从一个驱动器移到另一个驱动器上。这对我很有用:

Get-ChildItem" E:" - 递送| Move-Item -Destination" G:"