Powershell change

I have a powershell script below.  it grabs the latest txt file in a directory and then moves it to another directory.  basic copy and paste but only retrieving the latest file.

In my script i am naming the file a new name when i paste it into the desired folder.   I want it to keep the same name that it originally copied.  can someone show me how to do this.

example.. the following scipt may grab a file named dog.txt   but it always places this file as recent.txt in the final location.  i would like it to keep the original name of dog.txt

$dst = "\\contoso1\share"  # change to your source directory
$src = "C:\Users\john.doe\Desktop"  # change to your destination directory
$latest = (Get-ChildItem -Path $src -Filter *.txt -File |
    sort -Property LastWriteTime -Descending | select -First 1).FullName

Copy-Item -Path $latest -Destination (Join-Path -Path $dst -ChildPath recent.txt) -Force
jamesmetcalf74Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SeanSystem EngineerCommented:
Copy-Item -Path $latest -Destination (Join-Path -Path $dst -ChildPath $latest.txt) -Force

Should do the trick.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
Typically I would approach this as below.
$dst = "\\contoso1\share"  # change to your destination directory
$src = "C:\Users\john.doe\Desktop"  # change to your source directory
Get-ChildItem -Path $src -Filter *.txt -File |
 sort -Property LastWriteTime -Descending |
 select -First 1 |
 Copy-Item -Destination $dst -Force

Open in new window

jamesmetcalf74Author Commented:
Boom!
it worked.....
Thanks gentlemen
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.