Powershell script help copy and paste

I have a network drive that contains files that are created daily.  they are two types of files... .txt   .xml
I need to copy the newest .txt file and place it in a folder on my pc.
z:\   is the mapped drive that goes directly to the folder with the files.
c:\files  is my local drive and folder where i want the newest text file placed.
Can someone write this powershell command for me.

bonus question.  i set the execution policy to unrestricted
how do i make it so i can keep it restricted but allow certain saved ps scripts to run using "right click ->run" while keeping the global policy to restricted.
Who is Participating?

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

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.

Jose TorresCertified Database AdministratorCommented:
Try this out
$SrcDir = 'Z:\*'
$TrgDir = 'C:\Files\'

Get-ChildItem $SrcDir -File -Include @("*.txt", "*.xml") | Copy-Item -Destination $TrgDir

Open in new window

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
Jose TorresCertified Database AdministratorCommented:
Create a batch file in the same directory as the powershell script
Use the following code in the batch file (change ScriptName to your script)
The just double click the batch file to run
Powershell.exe -ExecutionPolicy Bypass ./ScriptName.ps1

Open in new window

Here's a mod of Jose's that filters to just the newest .txt file (by creation time).
$SrcDir = 'Z:\*'
$TrgDir = 'C:\Files\'

Get-ChildItem $SrcDir -File -Include "*.txt" | Sort CreationTime -Descending | Select -First 1 | Copy-Item -Destination $TrgDir

Open in new window

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Jose TorresCertified Database AdministratorCommented:
Thanks footech, didn't catch the newest file line.
But you are only including .txt file he also wants xml file
jamesmetcalf74Author Commented:
I have a really dumb question.
am I submitting the script correctly to powershell.
see how there is a new prompt after each line
Jose TorresCertified Database AdministratorCommented:
The version of powershell you are using may not support  the "-File" parameter.
Remove it from the code and it should work
@Jose - In the original question the asker said he only wanted to copy the newest .txt file, not .xml.

@jamesmetcalf74 - As Jose mentioned, looks like your PS version doesn't support that.  The -file parameter was added to Get-ChildItem in PS 3.0.
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

From novice to tech pro — start learning today.