hakk

software development, devops, and other drivel
Tree lined path

Powershell Change The Extension Of All Files In A Folder

Someone had to change the extension of some files in order to send them or upload them to share. That’s fine but there’s so many of them! It will take forever to rename them all manually. There must be an easier way. Let’s look at how to use PowerShell to change the extension of all files in a folder:

Open PowerShell:

  • Press Win + X and select “Windows PowerShell” from the menu.
  • Alternatively, you can search for “PowerShell” in the Start menu and open it.

Navigate to the Target Folder:

  • Use the cd command to navigate to the folder containing the files whose extensions you want to change.
  • For example, if your folder is located at C:\Users\YourUsername\Documents, you can navigate to it by typing:
    cd C:\Users\YourUsername\Documents
    

List Files in the Folder:

  • Use the dir command to list all the files in the current directory.
  • This will help you verify that you are in the correct folder and see the current extensions of the files.
  • For example:
    dir
    

Change Extensions:

  • Use the Rename-Item cmdlet to change the extension of each file.
  • Specify the current extension and the new extension you want to change to.
  • You can use a wildcard * to select all files with the specified extension.
  • For example, to change all .txt files to .md files, you can use the following command:
    Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '\.txt$','.md' }
    

Verify Changes:

  • After running the command, use the dir command again to verify that the extensions of the files have been changed as expected.

Exit PowerShell:

  • Once you have completed the task, you can exit PowerShell by typing exit and pressing Enter.

Note:

  • Ensure that you have appropriate permissions to rename files in the target folder.
  • Be cautious when running commands that modify files, especially if you’re not fully confident in what they do. Always make sure to have backups of your data.
  • You can modify the commands based on your specific requirements, such as changing the extensions to different ones or selecting files based on other criteria.