Share via


force overwrite with copy-item?

Question

Tuesday, October 7, 2014 2:56 PM

Is there a way to force silent overwrites with the powershell copy file cmdlet?

scott

All replies (4)

Tuesday, October 7, 2014 4:18 PM âś…Answered | 2 votes

Hi Tommy,

that is not quite correct:

Whether it prompts you for confirm by default is dependent on your confirmpreference setting (Try setting $ConfirmPreference to either "Low" or "High" and then try to overwrite a file using Copy-Item). Setting the -Confirm Parameter Overwrites the default settings. Thus, whenever you simply add -Confirm, it will prompt for Confirmation, no matter the ConfirmPreference, as you override the default and a set switch will toggle to $True (Not setting it is default, but you need to set it, in order to override the defaults). Appending the -Confirm Parameter and setting it to false (and you need to use the specified syntax to both set a switch and make it false) will override the defaults and cause the function to not prompt for confirmation.

Cheers,
Fred

There's no place like 127.0.0.1


Tuesday, October 7, 2014 3:04 PM | 2 votes

Have you tried using the -Force parameter? According to the help file (Get-Help Copy-Item), -Force, "Allows the cmdlet to copy items that cannot otherwise be changed, such as copying over a read-only file or alias."


Tuesday, October 7, 2014 3:28 PM | 2 votes

Hi Scott,

the surest way I know of ensuring silent operation is to specify both -Force and -Confirm:$False parameters.

Cheers,
Fred

There's no place like 127.0.0.1


Tuesday, October 7, 2014 3:42 PM | 1 vote

Nice catch, I didn't even think of using the -Confirm parameter. I checked the help (Get-Help Copy-Item -Parameter Confirm | select Name,Required) and its default is false, so in the case of the Copy-Item cmdlet the -Confirm parameter is not going to add anything.