Newbie: moving files

From Jeff Hicks videos and the help examples, I thought this would work

PS D:\> ls D:\temp2 -file | where LastWriteTime -gt (get-date).AddDays(-60) | Move-Item D:\temp3

But I get
“Move-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipe
line input or the input and its properties do not match any of the parameters that take pipeline input.”

What am I doing wrong?

I think you should use recurse switch after file parameter also use destination parameter after move cmdlet.

Always step into a script development, validating along the way to a final result.

ls D:\temp2 -file
will return this…
Directory: D:\Temp
Mode LastWriteTime Length Name

-a---- Wed 07 Jun 02017 512 CombinedSources07Jun2017.txt

PS D:> ls D:\temp2 -file | where LastWriteTime -gt (get-date).AddDays(-60)
Now filtering based on date, give you the same results, just only the files in the date range

ls D:\temp -file | where LastWriteTime -gt (get-date).AddDays(-60) | Move-Item D:\temp1
Now let’s move those file, wait, cannot do this because we have not yet told the Move-Item cmdlet specifically what file to move, because we are not passing a filename. We are pass everything at once. That is not a thing.

As per the examples in the PowerShell help files…
Example 1: Move a file to another directory and rename it
PS C:>Move-Item -Path C:\test.txt -Destination E:\Temp\tst.txt

This command moves the Test.txt file from the C: drive to the E:\Temp directory and renames it from test.txt to tst.txt.
Example 2: Move a directory and its contents to another directory
PS C:\>Move-Item -Path C:\Temp -Destination C:\Logs

This command moves the C:\Temp directory and its contents to the C:\Logs directory. The Temp directory, and all of its subdirectories and files, then appear in the Logs directory.
Example 3: Move all files of a specified extension from the current directory to another directory
PS C:\>Move-Item -Path .\*.txt -Destination C:\Logs

This command moves all of the text files (*.txt) in the current directory (represented by a dot (.)) to the C:\Logs directory.
Example 4: Recursively move all files of a specified extension from the current directory to another directory
PS C:\>Get-ChildItem -Path ".\*.txt" -Recurse | Move-Item -Destination "C:\TextFiles"

So, that where clause is the problem child and we need to change it to pass the filenames, not a directory list.

Get the filtered names
(ls D:\temp -file | where LastWriteTime -gt (get-date).AddDays(-60)).FullName


OK, now only getting files, let’s move them
(ls D:\temp -file | where LastWriteTime -gt (get-date).AddDays(-60)).FUllName | Move-Item D:\temp1
Dang error again, because we are trying to pass all at once, when we need to do this per filename.

(ls D:\temp -file | where LastWriteTime -gt (get-date).AddDays(-60)).FUllName |
% {Move-Item $_ D:\temp1}

Ahhhh, via a ForLoop, we iterate through the list and Move-Item likes that, because no errors
Let’s check
ls D:\Temp1

Directory: D:\Temp1

Mode LastWriteTime Length Name

-a---- Sat 18 Nov 02017 11686 fsoVolume.docx

Cool, mission accomplished.

But his post is not about recursion (root and all sub folders), just one folder filtered to send to another. So, adding recuse would just generate the same error; when what is happening is the poster is not handling the files themselves in the move. Move-Item requires source and destination. He has no source in the Move-Item pipeline, just a destination.

Yes, I suggest recurse if he missed it. Whatever, Move-item doesn’t require the source path in this case, as it taking input from pipe. But destination is mandatory to process.

If you look at Get-Help Move-Item it will show you what all you can pipe in: TypeName: System.IO.FileInfo is not one of those. I see Path or LiteralPath, both as strings.

Works for me

ls D:\temp2 -file | where LastWriteTime -gt (get-date).AddDays(-60) | Move-Item -destination D:\temp3

*Check your rights on filesystem

You have to say move-item -destination D:\temp3 because position 0 is for -path or the source in the help. Are you sure you want the newest files in the past 60 days? That’s what the code will move.