DEV Community

loading...

PowerShell Replace – How to Rename a File or Change a File Extension in Windows 10

Adeel Imran
・1 min read

Windows PowerShell is a Microsoft framework for automating tasks using a command line shell.

In this tutorial we will learn on how to change a file name or its extension using either the relative directory or absolute directory.

Here is a short 3 minute video in which I walk through how to change a file name or its extension.

We will be using Rename-Item cmdlet. You can read more in the official docs here.

Using Rename-Item we can not only rename a file but also move it into different directories or even change registry names as well.

You can find some very good official examples on the PowerShell docs page here.

So let's begin.

How to rename a file

Open your PowerShell command line shell. Type in the following command:

Rename-Item -LiteralPath "hello.xls" -NewName "bye.xls"
Enter fullscreen mode Exit fullscreen mode

Rename-Item accepts two flags:

  • LiteralPath is the file you want to rename
  • NewName is the new name of the file you want

How to change a file extension

You can change the extension of a file similar to how you would rename it by simply doing this:

Rename-Item -LiteralPath "hello.txt" -NewName "hello.docx"
Enter fullscreen mode Exit fullscreen mode

If you found this useful drop a message to me on twitter/adeelibr.

Happy coding.

Discussion (0)