Coding Ref

How to rename a folder in PowerShell

How to rename a folder in PowerShell

To rename a folder in PowerShell, you can use the Rename-Item cmdlet.

This cmdlet allows you to specify the path of the folder that you want to rename, as well as the new name for the folder.

For example, to rename the folder "C:\MyOldFolder" to "C:\MyNewFolder", you could use the following command:

Rename-Item -Path "C:\MyOldFolder" -NewName "MyNewFolder"

This would rename the folder from "C:\MyOldFolder" to "C:\MyNewFolder". Note that the -Path parameter specifies the current path of the folder, and the -NewName parameter specifies the new name for the folder, without the path.

Rename a folder and move it to a new location

If you want to rename the folder to a new location, rather than just changing its name, you can use the -Destination parameter of the Rename-Item cmdlet.

This parameter allows you to specify the new path and name for the folder, rather than just the new name.

For example, to rename the folder "C:\MyOldFolder" to "C:\MyNewFolder\MyRenamedFolder", you could use the following command:

Rename-Item -Path "C:\MyOldFolder" -Destination "C:\MyNewFolder\MyRenamedFolder"

This would rename the folder from "C:\MyOldFolder" to "C:\MyNewFolder\MyRenamedFolder", moving it to the "C:\MyNewFolder" directory in the process.

Overwriting an existing folder

The Rename-Item cmdlet will not rename a folder if the new name or destination already exists.

For example, if the "C:\MyNewFolder" directory already exists, the command above would fail, and you would need to specify a different new name or destination for the folder.

You can use the -Force parameter of the Rename-Item cmdlet to force the rename operation, even if the new name or destination already exists. However, this can cause data loss, so it should be used with caution.

Conclusion

To rename a folder in PowerShell, you can use the Rename-Item cmdlet.

This cmdlet allows you to specify the path of the folder that you want to rename, as well as the new name for the folder.

You'll also like

Related tutorials curated for you

    How to find special characters in PowerShell

    How to add a user to a group in PowerShell

    How to pause in PowerShell

    How to ping in PowerShell

    How to use Format-Table in PowerShell

    Using -Or in PowerShell

    What is IEX in PowerShell?

    String formatting in PowerShell

    What is PowerShell Get-ChildItem?

    How to rename a computer in PowerShell

    Switch statement in PowerShell

    How to sort and filter data in PowerShell