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.
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.
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.
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.
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.
if(Test-Path $file){
prompt user and copy if yes
}else{
Copy file
}
copy-item file1 file2 -confirm:$(Test-Path file2)is one way, but not too convenient at the command prompt.
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" ?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"
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
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
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"
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.
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 <##>