Greetings Powershell Experts –
I have a folder with 10 files (I’m using .pst files in this instance) in it. I want to use powershell to select the newest file and copy it to a different folder. Here is what I thought the code would look like:
Get-childitem c:\source\*.pst | sort-object lastwritetime –descending | select-object –first 1 | copy-item c:\target
When I run this, I get the following error
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At :line:3 char:34
+ Select-Object -First 1 | Copy-Item <<<< c:\target
I read this as basically saying I can’t pipe input into the copy-item commandlet. My next thought was to use a variable, so I tried this code:
$item = Get-childitem c:\source\*.pst | sort-object lastwritetime –descending | select-object –first 1
Copy-item $item c:\target
When I do this, the file is copied, but the powershell script looks like its caught in a loop and I have to manually stop it (I’m using PowerGui to write the script).
What am I doing wrong? Any help would be greatly appreciated.
Thanks in advance,