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.
jamesmetcalf74Asked:
Who is Participating?
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 TorresSenior SQL Server DBACommented:
Try this out
$SrcDir = 'Z:\*'
$TrgDir = 'C:\Files\'

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

Open in new window

0

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 TorresSenior SQL Server DBACommented:
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

0
footechCommented:
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

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Jose TorresSenior SQL Server DBACommented:
Thanks footech, didn't catch the newest file line.
But you are only including .txt file he also wants xml file
0
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
results-of-footech-scriptJPG.JPG
0
Jose TorresSenior SQL Server DBACommented:
The version of powershell you are using may not support  the "-File" parameter.
Remove it from the code and it should work
0
footechCommented:
@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.
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.