Learn about Windows PowerShell
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.
“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.
“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:
The command and the output from the command are shown in the image that follows.
“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.
“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:
When the command appeared in the Windows PowerShell console window, she pressed ENTER to run the command. Nothing came back.
“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.
Copy-i<tab><space> -p<tab><space>c:\fso<space>-fi<tab><space>*.jpg<space> -d<tab><space> c:\fsox<space>-r<tab><enter>
Copy-Item -Path C:\fso -Filter *.jpg -Destination c:\fsox –Recurse
The newly copied files are shown in the following image.
“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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
How about copying files to remote hosts? What if the remote file exists and it's read only?
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.
I started a project to help improve the copy options in Powershell.
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 ...'.
@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. blogs.msdn.com/.../long-paths-in-net-part-1-of-3-kim-hamilton.aspx
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.
I have download Long Path Tool and it is the best solution of your problem.
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?