How to rename / move an element and redefine even if they exist (for files, folders and links)?

This seems to be a trivial question and has been answered several times. Nevertheless, I do not understand.

Is there one command to move an item (or just rename it as the source and destination are in the same folder) with forced rewriting that works for any type of item (sheet, container)?

Reference Information. I am writing a script that replaces all hard links and connections with the corresponding corresponding symbolic links. Code example:

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'

#produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force

#unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force

#this works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'
+4
source share
1 answer

, , . , , Microsoft.

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'

#produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force

. , .

#unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force

. , , .

#this works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'

, Microsoft .

, .Net System.IO

0

Source: https://habr.com/ru/post/1670928/


All Articles