Use PowerShell to Copy Files and Folders to a New Location

Summary: The Scripting Wife learns about using Windows PowerShell to copy files and folders in prep for the 2013 Scripting Games.

The days seem to start early, and the evening arrives later this time of year in Charlotte, North Carolina. I am sitting on the lanai drinking a cup of English Breakfast tea, and checking my email with my Microsoft Surface RT device. In my mind, it is a perfect way to ease in to the day, and my Surface is great for stuff like checking email and sending tweets.

I hear a sort of a thump, thump, thump, rattle, rattle, rattle sound and I look up. Strangely enough, I see nothing, so I go back to reading my email. But the sound persists. I get up from my swing and go into the house. I see the source of the noise: the Scripting Wife is digging through the front closet, and she has boxes, books, and other assorted paraphernalia spread out over the vestibule floor. In situations like this, I have learned to keep my mouth shut, and to wait for a natural opening.  

“I can’t find the flippin’ book,” the Scripting Wife exclaimed without looking up.

Not sure if this is a question, a remark, or even if it was directed at me, I decide to keep quiet.

“Well are you going to help?” she asked as she stood up.

“How can I help you, my peach?” I implored.

“You can help me find my autographed copy of Don’s book.”

I wait, knowing there is more to come.

“I saw your blog post about the upcoming 2013 Scripting Games, and I need to get back into shape by writing some code,” she stated.

“You are sort of like a scripting butterfly. You go here and there in random directions looking for nectar, but you never light for very long,” I paused to catch my breath.

“But I don’t need to know how to write scripts when I have my very own script monkey. Besides I just do this for fun,” she said.

“Well, I have no idea where you put your book, but I can help you for a few minutes if you wish,” I offered.

“I saw you reading your email, and I did not want to disturb you,” she said.

“Then why all the noise?” I asked.

“Maybe I figured if you came in here, then you were no longer reading your email and I could get you to help,” she posited.

“So what do you need to do?” I asked.

“Tell me about copying files. I have a bunch of pictures on my laptop that I want to copy up to the SAN so they do not get lost,” she said.

“Grab your laptop and come out to the lanai,” I instructed.

Using Copy-Item to copy files

“To copy a file from one folder to another, what cmdlet do you think you might use?” I asked.

Copy-File,” she posited.

“Close. It is called Copy-Item,” I said.

“Oh, yeah. Now I remember,” she said.

“Open your Windows PowerShell console, and use Copy-Item to copy any file from your C:\fso folder to your C:\fsox folder,” I said.

She thought for about a second and typed the following until she had a file selected:


Next she typed the following:


The completed command is shown here:

Copy-Item -Path C:\fso\20110314.log -Destination c:\fsox\mylog.log

When she pressed ENTER, an error arose. The command and the error message are shown in the following image.

Image of error message

“Well, that did not work,” she said, “I guess I do need to go find Don’s book after all.”

“No, I wanted to show you what happens if the destination folder does not exist,” I said.

“How can I avoid that problem?”

“You can use the Test-Path cmdlet. Type Test-Path and supply the path to the folder,” I said.

She typed the following into the Windows PowerShell console:


The command is shown here:

Test-Path c:\fsox

The command and the output from the command are shown in the image that follows.

Image of command output

“So the command says False. I guess that means the folder does not exist?” she asked.

“Yep. That is right. Now use the MD command and give it a path so that you create the new C:\fsox folder,” I said.

She typed the following:


The command is shown here.

md c:\fsox

The command and the output from the command are shown in the image that follows.

Image of command output

“Now use the Up arrow and retrieve your previous Copy-Item command,” I said.

The Scripting Wife pressed the Up arrow a few times until the following command appeared:

Copy-Item -Path C:\fso\20110314.log -Destination c:\fsox\mylog.log

When the command appeared in the Windows PowerShell console window, she pressed ENTER to run the command. Nothing came back.

Easily copy multiple files by using Copy-Item

“That is OK, but I specifically want to copy my pictures, and I do not want to type a bunch of names. So how can I make Windows PowerShell copy my pictures for me?” she asked.

“To do that, use the Filter parameter and specify .jpg, which is the extension you use for your pictures. You will also need to specify the Recurse parameter,” I said.

She typed the following:

Copy-i<tab><space> -p<tab><space>c:\fso<space>-fi<tab><space>*.jpg<space> -d<tab><space> c:\fsox<space>-r<tab><enter>

The command is shown here:

Copy-Item -Path C:\fso -Filter *.jpg -Destination c:\fsox –Recurse

The newly copied files are shown in the following image.

Image of menu

“Cool,” she said as she bounded through the front door.

Scripting Games Prep Week will continue tomorrow when I will talk about Windows Management Instrumentation and obtaining basic computer information.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (15)

  1. Greg Wojan says:


    Copying to remote hosts should work just the same as long as you have permissions to write into the destination. If you encounter READONLY files use 'cp -force …'.

  2. jrv says:

    @snarf –

    You know that is not really true.  The link you posted seems to prove that.

    If we use the \?<drive><path> method ther is no issue.

  3. Jose says:

    How about copying files to remote hosts? What if the remote file exists and it's read only?

  4. Lionel says:

    Copying with PowerShell is nice at first.  Then you hit a long path ( > 256 characters (or something like that)), and it fails horribly, and you go back to robocopy while cursing the developers for failing to support this in an otherwise great tool.

  5. Chris says:

    I started a project to help improve the copy options in Powershell.

  6. Snarf says:

    @Lionel, Microsoft doesn't really support long file names and paths. They've just hacked Windows so it feels like it does to the average user.…/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

  7. rebeccazoly says:

    I have download Long Path Tool and it is the best solution of your problem.

  8. AdamW says:

    I tried the above Copy-Item -Path C:fso -Filter *.jpg -Destination c:fsox –Recurse and it worked fine, but it also brought over all directories regardless if they had a jpg in them or not. If they did not have a jpg in them they were empty …

    Any way to not copy directories if they do not meet the filter criteria?

  9. Ketsusa says:

    @AdamW Use RoboCopy?

    Robocopy Options and Switches
    Copy options :
    /S :: copy Subdirectories, but not empty ones

  10. IJ says:

    How to replace existing files with new folders/files?

  11. thomas says:

    Please tell me faster method to copy files to remote servers as I tried to copy files by creating a network drive and its slow

  12. justin says:

    How do you scan multiple drives for a file and then copy it?

  13. justin says:

    I am trying to configure a Veeam PS Script. I’ve gotten to the point where it mounts the backup, but the copy portion isn’t working fully, as it doesn’t copy the file.

    I’m using what appears to be Test-Path, but I’m not entirely sure how to get it to scan the mounted "volumes" and then once it finds the file copy it to the desired location. Here’s the script:

    $computer = "Computer01"
    $jobname = "Job01"
    $origfile = "_Important Files"
    $restoreto = "E:Sample Location"
    $result = Get-VBRBackup | where {$_.jobname -eq "$jobname"} | Get-VBRRestorePoint | where {$ -eq "$computer"}| Where-Object {$_.CreationTime -like "*8/24/2015*"} | Select-Object -First 1 | Start-VBRWindowsFileRestore

    $folders = gci "c:veeamflr$computer" | select-object FullName
    foreach($folder in $folders){
    if(Test-Path ($folder.fullname +(Split-Path -NoQualifier $origfile))) {
    $file = ($folder.fullname +(Split-Path -NoQualifier $origfile))
    Copy-Item $file $restoreto -Recurse
    Stop-VBRWindowsFileRestore $result

  14. Dan_IT says:

    How about building a script to create a new folder, prompt the user to name the folder, then copy/move files into the new folder from an external drive. Is this possible? Thanks.

Skip to main content