Coding Ref

How to rename a file in PowerShell

How to rename a file in PowerShell

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

This cmdlet takes the path and current name of the file as input, and the new name for the file as an argument. It then renames the file to the specified new name.

Example

For example, to rename a file named myfile.txt to mynewfile.txt in the current directory, you can use the following PowerShell command:

Rename-Item "myfile.txt" "mynewfile.txt"

This command will rename the myfile.txt file in the current directory to mynewfile.txt.

The original file will be replaced by the new file with the specified name, and any existing file with the same name will be overwritten.

It is important to note that the `Rename-Item` cmdlet only renames the file itself, and does not move the file to a different location.

If you want to rename a file and move it to a different directory at the same time, you can use the Move-Item cmdlet instead.

This cmdlet takes the path and current name of the file as input, and the new path and name for the file as arguments. It then renames the file and moves it to the specified location.

Rename and move a file

For example, to rename a file named myfile.txt to mynewfile.txt and move it from the current directory to the C:\myfiles directory, you can use the following PowerShell command:

Move-Item "myfile.txt" "C:\myfiles\mynewfile.txt"

This command will rename the myfile.txt file in the current directory to mynewfile.txt and move it to the C:\myfiles directory.

The original file will be replaced by the new file with the specified name and location, and any existing file with the same name and location will be overwritten.

Conclusion

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

You'll also like

Related tutorials curated for you

    How to use SFTP in PowerShell

    How to create new items in PowerShell

    How to select an object in PowerShell

    Not equal to in PowerShell

    How to wait in PowerShell

    How to move items in PowerShell

    How to list all installed modules in PowerShell

    String formatting in PowerShell

    What is null in PowerShell?

    How to sort in PowerShell

    How to order a hashtable in PowerShell

    How to rename a file in PowerShell