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

问题描述 投票:15回答:4

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

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

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

powershell powershell-v2.0 powershell-v3.0 powershell-v1.0
4个回答
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


16
投票

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

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

7
投票

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


0
投票

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

Get-ChildItem“E:” - Recurse | Move-Item -Destination“G:”

© www.soinside.com 2019 - 2024. All rights reserved.