17

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1926332
  • 685
  • 2
  • 9
  • 13

4 Answers4

21

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

robocopy \\servername\location C:\Dumps /e /mov /minage:31
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • This script is copying files to the destination and only from the main path. I would like to include the subfolders as well. I want to move them to another location. – user1926332 Feb 18 '13 at 12:17
  • 1
    Use `/s` to recurse into subfolders. Use `/e` (instead of `/s`) to include empty subfolders as well. What do you mean by "another location"? Do you not want to preserve the folder structure? In that case you could stick with PowerShell (with the `-Recursive` option, as @Musaab mentioned). This approach will put you at risk of data loss due to overwritten files, though. – Ansgar Wiechers Feb 18 '13 at 12:31
  • Thanks Ansgar....your robocopy script did the job i wanted and more over I was able to build one more script for another usage. – user1926332 Feb 19 '13 at 15:51
  • 4
    robocopy does the move **by copying file bit by bit and then deleting it**. Instead just moving it quickly. Hence is much slower. This behavior cannot be turned off. – peterson Sep 30 '15 at 11:30
  • @peterson How would you move files between different filesystems without doing that? – Ansgar Wiechers Sep 30 '15 at 14:26
  • 3
    @AnsgarWiechers this behaviour occurs when moving on same filesystem, within folders. Because afaik nothing else uses this strange and slow approach, it's worth to mention – peterson Oct 01 '15 at 17:48
17

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Musaab Al-Okaidi
  • 3,734
  • 22
  • 21
  • Musaab. The -Recursive doesn't seem to be working. Can you help further ? – user1926332 Feb 18 '13 at 12:35
  • Are you sure the files meet the where-object clause? I'll update my answer with a screenshot to illustrate how I'm using the -Recursive option. Check it and let me know what I'm I doing different. – Musaab Al-Okaidi Feb 18 '13 at 12:51
  • Hello Musaab...I have come across a new challenge now....with the above script...i am trying to schedule this in windows task scheduler (windows 2008 server X84 bit) on schedule the powershell script isn't working...manually running on batch it does the job. can you please help me further...I have placed lot of arguments to make that run..but nothing worked out....please help... – user1926332 Feb 26 '13 at 14:17
  • Have a look at my answer to this question, it will probably help. http://stackoverflow.com/questions/14635995/running-powershell-script-as-scheduled-task/14644128#14644128 – Musaab Al-Okaidi Feb 26 '13 at 15:19
  • 4
    This doesn't preserve the folder structure – Sam Holder Apr 15 '16 at 16:16
8

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

0

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

Vinny
  • 11
  • 1