Help copying a file

For all you experts out there I am sorry if this is a stupid question but I am trying to teach myself PowerShell and I cannot figure out why something is not working.

My goal is the copy the newest file in a directory, out 0f the directory to another directory.

I figured out if I run these two commands (below) interactively, it works but when I try to run them in a script It fails with this error on the Copy-Item command
<p style=“padding-left: 40px;”>Copy-Item : A positional parameter cannot be found that accepts argument ‘System.Object[]’.
At line:1 char:1</p>

PS C:\ $newestfile = Get-ChildItem -path “C:\Users\test\Unifi Backups\dom”| Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

PS c:\ Copy-Item -path $newestfile -destination “c:\junk”

Can someone give this newbie a suggestion how to make this work?

Thanks a lot

Look at the contents of $newestfile…

You’ll see it is an object with various pieces of information, Mode, LastWriteTime, Length and Name. But what you need is FullName, eg ‘C:\Users\test\Unifi Backups\dom\filename.txt’, that you can pass to copy-item.

So add a select FullName. However, you also need to allow for more than one file being returned. Or state that you only want 1 file name returned. Lets assume you want all returned and to copy them all…

(Get-ChildItem -path 'D:\temp' | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-14)}).FullName | ForEach {
Copy-Item -path $_ -destination 'D:\temp2'



Chances are there is more than one file. Regardless you can just pipe it into copy-item

Get-ChildItem -path “C:\Users\test\Unifi Backups\dom”|
    Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}|
        Copy-Item -destination “c:\junk”

all Thank for your response… I am just learning… I missed the the “fullname”.

so did this
<p style=“padding-left: 40px;”>PS C:&gt; $newestfile = (Get-ChildItem -path “C:\Users\mandgphoto\OneDrive\Unifi Backups\dom”| Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}).fullname</p>
and it gave me this
<p style=“padding-left: 40px;”>C:\dom\Backup
This is perfect but
<p style=“padding-left: 40px;”>c:\dom\backup - is a directory
I do not need the JSON file</p>
How would I choose just the file with the .unf extension and copy only that to a new directory

Also can someone recommend a place where I can read/learn about how to build these types of commands in powershell. The next thing I need to do it to find stings of numbers (serial number) in a filename and then copy the corresponding file to a new directory based on that number

thanks again for all of your


You can use the Filter

Get-ChildItem -path "C:\Users\mandgphoto\OneDrive\Unifi Backups\dom" -filter "*.unf" 

You can ignore json files

Get-ChildItem -path "C:\Users\mandgphoto\OneDrive\Unifi Backups\dom" -exclude "*.json" 

everyone has been so helpful… thank you!

There is nothing like learning powershell the hard way by reading and experimenting but there aer just some things that is hard to do it that way… Is there someplace where I can learn how get better with Powershell. I do not mind paying a little money but I tried one of the the oreily classes and it was way to simplistic for me

thanks for any pointers or suggestions


I am learning it like you:

  • Starting writing scripts and asking google for solving a problem.
  • Found a solution, reading and (trying) understanding the solution.
  • Remember the solution in own code, at the same problem
  • Reading in forums other people problems and the solutions.
  • I startet with little helper scripts and now I do all my commands in Modules
  • man Get-Childitem -full or man Get-Childitem -examples helps
  • (Update-Help was needed)
To "develope" I use "Visual Studio Code" from MS. It is free and a good tool. For me -as a Company-Admin- there where a lof of things I have to do: "Put me in this Distributions Group": I did and then I developed a little routine for putting people into a DistributionGroup. And removing him/her. "Create a ner DistributionGroup: I did and then I developed a little routine for it.

It is a language. Speak it and it will train you.

There are a lot of tutorials out there. But I am not able to read them like a cookbook and understand it. Google and forums helps me more.