Search code examples
powershellfile-renamewindows-server-2016

PowerShell Rename-Item is creating a copy of the file


My script is supposed to rename c:\myfolder\myfile.txt to myfile.bak

Rename-Item "c:\myfolder\myfile.txt" -NewName "myfile.bak"

However, it leaves myfile.txt in place and creates a new file called myfile.bak instead

Does anyone know why?


Solution

  • If its a hidden or read-only file, you have to add the -force parameter:

    Forces the cmdlet to rename items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables. The cmdlet cannot change constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers.