PowerShell: How to Add Prefix to All Files in Folder

Often you may want to use PowerShell to add a specific prefix to all files in a folder.

You can use the following basic syntax to do so:

(Get-ChildItem -File) | Rename-Item -NewName {$_.Name -replace '^','new_'}

This particular example adds the prefix new_ to all files in the current directory.

Here is what the various commands do:

  • Get-ChildItem -File: Retrieve all files in current directory
  • Rename-Item -NewName: Specifies that all files should be renamed
  • $_.Name: Gets the existing filename
  • -replace: Specifies that the start ( ^ ) of the file should be replaced with new_

The following example shows how to use this syntax in practice.

Example: Use PowerShell to Add Prefix to All Files in Folder

We can use the Dir command to first view all files in our current directory:

We can see that our current directory has the following files:

  • athletes.txt
  • coaches.txt
  • managers.txt

Suppose that we would like to add the prefix new_ to the beginning of each file in our current directory.

We can use the following syntax to do so:

(Get-ChildItem -File) | Rename-Item -NewName {$_.Name -replace '^','new_'}

The following screenshot shows how to use this syntax in practice:

PowerShell add prefix to all files in folder

When we use the Dir command again we can see that the current directory now contains files with the following names:

  • new_athletes.txt
  • new_coaches.txt
  • new_managers.txt

Notice that new_ has been added as a prefix to each file.

Feel free to replace new_ with whatever prefix you would like to add to your own files.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Get File Name from Path
PowerShell: How to Add Date to File Name
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply