Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Copy-Item

Reply
 
Old 05-03-2007   #1 (permalink)
JW
Guest


 
 

Copy-Item

How do I prevent Copy-Item from overwriting an exsisting file

My System SpecsSystem Spec
Old 05-03-2007   #2 (permalink)
Keith Hill
Guest


 
 

Re: Copy-Item

"JW" <JW@discussions.microsoft.com> wrote in message
news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> How do I prevent Copy-Item from overwriting an exsisting file


It shouldn't do that by default unless you specify the -force parameter.

--
Keith

My System SpecsSystem Spec
Old 05-03-2007   #3 (permalink)
JW
Guest


 
 

Re: Copy-Item

Well on my system it does. According to the documentation the -force switch
only:

"Allows cmdlet to override restrictions such as renaming existing files as
long as security is not compromised"

Which is in my opinion needed when attributes like 'hidden' etc. are
specified.

The command:

Copy-Item C:\test.txt -Destination C:\test\

happily copies test.txt into the C:\test directory clobbering the exsisting
test.txt already present in C:\test

--
JW


"Keith Hill" wrote:

> "JW" <JW@discussions.microsoft.com> wrote in message
> news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> > How do I prevent Copy-Item from overwriting an exsisting file

>
> It shouldn't do that by default unless you specify the -force parameter.
>
> --
> Keith
>

My System SpecsSystem Spec
Old 05-03-2007   #4 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Copy-Item

"JW" <JW@discussions.microsoft.com> wrote in message
news:530C457C-4110-44CC-A556-F1B7B045E2B9@microsoft.com...
> Well on my system it does. According to the documentation the -force
> switch
> only:
>
> "Allows cmdlet to override restrictions such as renaming existing files as
> long as security is not compromised"
>
> Which is in my opinion needed when attributes like 'hidden' etc. are
> specified.
>
> The command:
>
> Copy-Item C:\test.txt -Destination C:\test\
>
> happily copies test.txt into the C:\test directory clobbering the
> exsisting
> test.txt already present in C:\test


Yeah I see the same behavior. It is Move-Item that requires -force to
overwrite a file. Seems a bit inconsistent that Copy-Item doesn't behave
the same way IMO.

--
Keith


My System SpecsSystem Spec
Old 05-03-2007   #5 (permalink)
IJuan
Guest


 
 

Re: Copy-Item

Here is a quick little function I just made for a work around. If you dot
source it you can use it when ever you need it.

function SafeCopy {
param ([string]$destination = $(throw "Please specify a destination"),
$confirm = $true)
Process {
$fulldestination = (Get-Item $destination).FullName + '\' + $_.Name
if (Test-Path $fulldestination) {
if ($confirm) {
Write-Warning "File exists"
Copy-Item $_.FullName $fulldestination -confirm
}
}
else {Copy-Item $_.FullName $fulldestination}
}
}


I made it to work in the pipeline so that I didn't have support all the
switches that Copy-Item does. Just use it like this:

PS> Get-ChildItem test.txt | SafeCopy c:\temp $false

That way you can use all the switches of Get-ChildItem and even pass it to
Where for further filtering before actually doing your copy.

-= IJuan =-

"JW" wrote:

> Well on my system it does. According to the documentation the -force switch
> only:
>
> "Allows cmdlet to override restrictions such as renaming existing files as
> long as security is not compromised"
>
> Which is in my opinion needed when attributes like 'hidden' etc. are
> specified.
>
> The command:
>
> Copy-Item C:\test.txt -Destination C:\test\
>
> happily copies test.txt into the C:\test directory clobbering the exsisting
> test.txt already present in C:\test
>
> --
> JW
>
>
> "Keith Hill" wrote:
>
> > "JW" <JW@discussions.microsoft.com> wrote in message
> > news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> > > How do I prevent Copy-Item from overwriting an exsisting file

> >
> > It shouldn't do that by default unless you specify the -force parameter.
> >
> > --
> > Keith
> >

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copy-Item : Container cannot be copied onto existing leaf item. PowerShell
Can the copy-item cmdlet be used to copy public folders to C: ? PowerShell
copy-item changing files attributes on network copy failures PowerShell
Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? PowerShell
Copy-Item or Copy-ItemProperty and Remote Registry. PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46