Can Copy-Item require confirmation when the destination already exists ?
Can Copy-Item, when invoked at the PowerShell command line, be made to
require confirmation when the destination item already exists ?

Specifically, I would like Copy-Item to work like cmd.exe's internal COPY command with respect to requesting permission to overwrite a file in a File System context when used at a command prompt.

I see a -Confirm switch for Copy-Item, but that requires confirmation even if the destination item does not exist.


August 20th, 2015 4:07pm

As it stands, I don't believe so.

You could write a replacement that uses Test-Path to verify if the file already exists and then prompts for confirmation before doing the copy though.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:15pm


You could write a replacement that uses Test-Path to verify if the file already exists and then prompts for confirmation before doing the copy though.

The tricky part is restricting the confirmation to the command line interactive use, while allowing the usual behavior when Copy-Item is used in a script.  

I have added that sort of logic to a replacement for the dir alias of Get-ChildItem but I have not been absolutely happy with my design when processing pipelined input.
August 20th, 2015 5:02pm

if(Test-Path $file){
   prompt  user and copy if yes

}else{
     Copy file
}

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:05pm

copy-item file1 file2 -confirm:$(Test-Path file2)
is one way, but not too convenient at the command prompt.


August 20th, 2015 10:05pm

copy-item file1 file2 -confirm:$(Test-Path file2)
is one way, but not too convenient at the command prompt.


As stated earlier Copy-Item does not support "confirm"

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 11:15pm

copy-item file1 file2 -confirm:$(Test-Path file2)
is one way, but not too convenient at the command prompt.


As stated earlier Copy-Item does not support "confirm"


 jrv, what do you mean by "support" ?
August 21st, 2015 1:00am

copy-item file1 file2 -confirm:$(Test-Path file2)
is one way, but not too convenient at the command prompt.


  • Marked as answer by LarryWeiss 6 hours 17 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 1:59am

copy-item file1 file2 -confirm:$(Test-Path file2)
is one way, but not too convenient at the command prompt.


  • Marked as answer by LarryWeiss Thursday, September 03, 2015 12:53 AM
August 21st, 2015 1:59am

          copy-item file1 file2 -confirm:$(Test-Path file2)
                 is one way, but not too convenient at the command prompt.


As stated earlier Copy-Item does not support "confirm"

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 10:46pm

So you are saying that the Help system has an answer that is reliable?  I suspect that, if you try it, it will not work.

Don't believe everything you read on the Internet.

September 1st, 2015 11:25pm

Your suspicions are unwarranted:

<##> copy-item file1 file2
<##> copy-item file1 file2 -confirm

Confirm
Are you sure you want to perform this action?
Performing operation "Copy File" on Target "Item: C:\users\larry\documents\windowspowershell\file1 Destination:
C:\users\larry\documents\windowspowershell\file2".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
<##>

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 3:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics