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