PowerShell script to move files and folders includ

2019-04-18 17:27发布

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.

4条回答
乱世女痞
2楼-- · 2019-04-18 17:40

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:

screenshot

查看更多
聊天终结者
3楼-- · 2019-04-18 17:45

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

robocopy \\servername\location C:\Dumps /e /mov /minage:31
查看更多
▲ chillily
4楼-- · 2019-04-18 17:51

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

查看更多
Anthone
5楼-- · 2019-04-18 17:54

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:"

查看更多
登录 后发表回答