header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Copy-Item versus XCOPY
Last Post 15 Jan 2008 01:56 AM by Poseidon88. 10 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
31 Dec 2007 10:10 PM  

I'm trying to develop a script using PowerShell commands but I am having difficulity with COPY-ITEM.

I want to copy a folder and all of its substructures to another destination folder name.  I wish to overwrite the destination folder name every time.

Copy-Item ./Foldername ./Othername

Does not seem to work for me.  Is there a trick that I am missing?  I tried the get-help copy-item and used the example.

Would XCOPY be better?

My blog: http://blogs.powershellcentral.com/kscriss/
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
31 Dec 2007 10:30 PM  
It may help if you provided the error message.

No, I see no reason to revert to xcopy necessarily.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
31 Dec 2007 10:43 PM  
I have a sample script that may help you

http://www.bsonposh.com/modules/wordpress/?p=38
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
02 Jan 2008 02:11 PM  
You should be able to use:

copy c:\source z:\dest -recurse -force
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
02 Jan 2008 02:37 PM  

copy ./SolarWinds ./SolarStorms -recurse -force

I try this command above.  The first time it works and makes a copy of the Solarwinds directory.  The next time I run it it moves the SolarWinds directory into the SolarStorms directory.  That's not right is it?

Anyway I need a way to do remote copies of folders on remote servers.  I'll probably use PSEXEC and XCOPY unless there is a better suggestion.

My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
02 Jan 2008 02:53 PM  

I didn't mean move in the move sense of the word.  I ment it copies the Solarwinds dirctory one level down into the solarstorm directory.

My blog: http://blogs.powershellcentral.com/kscriss/
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
02 Jan 2008 03:18 PM  
Try using complete paths. When I run this command:

copy c:\test c:\temp\test_copy -recurse -force

The contents of c:\test are copied to the test_copy directory.
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
jbrinkmanUser is Offline
New Member
New Member
Posts:5

--
02 Jan 2008 03:19 PM  

I suspect that your issue is that you do not want to copy ./SolarWinds (which is a directory) but rather wish to copy the contents of the directory which is ./SolarWinds/*.* . I think that this is one of the problems with letting the computer try to "guess" your intent.  Sometimes it guesses correctly, and sometimes it does not.

If you do ultimately decide to go the xcopy route, then you should probably use RoboCopy instead.  It is a much more robust and modern version of the XCopy command which has been deprecated in the latest Microsoft OS's

Joe Brinkman
--------------------------------------------------------
ASP.Net MVP
INETA Speaker
VP Core Technology, DotNetNuke Corp
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
02 Jan 2008 03:29 PM  
Well this isn't working 100% as I thought it was. It works fine the first time. I modified one of the files in c:\test and tried the copy command again. PowerShell shows everything copied (I used -passthru). But the modified file isn't overwriting the destination file. But it did work if I manually copied just the one file.

I wonder what's going on.
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
02 Jan 2008 03:34 PM  
Nevermind. Now I see what you are talking about. The second time I copied, the files were getting sent to c:\temp\test_copy\test.
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
Poseidon88User is Offline
New Member
New Member
Posts:1
Avatar

--
15 Jan 2008 01:56 AM  

I've run into the same problem.  Copy-Item seems to be a bit buggy.  It behaves very differently depending on whether the destination exists or not.  So, for instance if I have a folder c:\test\a that contains a text file:

Copy-Item c:\test\a c:\test\b -recurse

if c:\test\b does not exist, it will be created, and its contents will be identical to c:\test\a.

if c:\test\b does exist, a copy of c:\test\a will be created inside c:\test\b.

On the other hand:

Copy-Item c:\test\a\* c:\test\b -force

works fine for either case.  But, now suppose that c:\test\a has a sub-directory called aa.  If you run the same command, and b does not exist, it will create b and just copy over the file contents of a.  But if b does exist, it will copy over the file contents, and create any top-level sub-directories.  And it gets even weirder if you use -recurse.  In that case, If b does not exist, instead of copying the directory structure, it will copy all the files in the tree to the root level of b.

Weirdness.  I think the only solution is to check beforehand whether the target location exists using test-path, then adjust your copy call accordingly.

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer