Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

PS: Move-Item. How to force to overwrite an existing file?

Hi,


I use this PS cmd:


Get-ChildItem -Path I:\IT-UsersToDisable -File | Move-Item -Destination I:\IT-UsersToDisable\OLD


But got this error msg:
Move-Item : Cannot create a file when that file already exists.

I realized that file: users.csv is already existed in "E:\IT-UsersToDisable\OLD"

How can I force it to just move and overwrite it?

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

What is weird is the Move-Item cmd with -force doesn't work in pipeline.


where are you adding the -force -destination ... as the first argument?
Avatar of SAM2009

ASKER

Like this:

Get-ChildItem -Path I:\IT-UsersToDisable -File | Move-Item -Destination I:\IT-UsersToDisable\OLD -Force
Did this work, or did not work?
If did not, does including it at the begining change the behavior?
Or in this case you have to pass foreach-item and then run move-item one at a time?
OLD is a folder, I assume?
The command should work. To make sure, I even tried with read-only files in both locations - no issue.
There might be an issue with system files or files with a particular security setting on files or folders.

BTW, the sequence of (named) options does not matter. The sequence of unnamed parameters does, of course ;-).
Avatar of SAM2009

ASKER

Does it work like in my suggested cmd? Like with Get-ChildItem first and add the end: "| Move-Item...-Force"?
Yes.  My test was something like
gci *.txt | move-item -force -Destination Old   

Open in new window

Look what the get-childitem outputs

I do not think you need to specify what is being moved when it is fed over pipe, is the issue where -force is added, at the beginig as the first argument or after the last.
Are there spaces in the file names.
Avatar of SAM2009

ASKER

Thank you. Finally after comparing files I realize there a NTFS security issue. Thanks for yoir help.