Skip to content

Add prefix and suffix to all files in folder with PowerShell

Many files are in a folder, and you must add a prefix or suffix to all file names. Going through them one by one in File Explorer and adding a prefix or suffix is time-consuming. An excellent way to add prefixes and suffixes to multiple file names is with PowerShell. In this article, you will learn how to add a prefix and suffix to all file names with PowerShell.

Get all files in folder

To get all the files in a folder, run the below commands.

In our example, it’s the folder Documents in location C:\Temp\Documents.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder)

The output will show the files in the folder.

    Directory: C:\Temp\Documents


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         6/13/2022  10:04 AM              8 About.txt
-a----         6/13/2022  10:05 AM           8457 Data.xlsx
-a----         6/13/2022  10:04 AM            248 Info.txt
-a----         6/13/2022  10:02 AM          11936 Plan.docx
-a----         6/13/2022  10:03 AM           8365 Pricing.xlsx
-a----         6/13/2022  10:04 AM              3 Release.txt
-a----         6/13/2022  10:02 AM          11936 Stage 1.docx
-a----         6/13/2022  10:02 AM          11936 Stage 2.docx
-a----         6/13/2022  10:02 AM          11936 Stage 3.docx

This is what the files in the folder look like in File Explorer.

Prefix suffix PowerShell all files in folder

Add prefix to all files in folder

To add a prefix to all files in a folder, run the below commands.

In our example, the prefix Project 1 – is added to all the files in the specific folder Documents.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder) | Rename-Item -NewName {"Project 1 - " + $_.Name}

This is what the files in the folder look like in File Explorer.

Prefix suffix PowerShell add prefix

Add suffix to all files in folder

To add a suffix to all files in a folder, run the below commands.

In our example, the suffix – Cloud is added to all the files in the folder Documents.

Note: This will place the suffix after the file name and keep the file extension in place.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder) | Rename-Item -NewName {$_.BaseName + " - Cloud" + $_.Extension}

This is what the files in the folder will look like in File Explorer.

Prefix suffix PowerShell add suffix

Replace text from files in folder

To replace text from all files in a folder, run the below commands.

In our example, we will replace – Cloud with – Local.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder) | Rename-Item -NewName {($_.Name).Replace(" - Cloud"," - Local")}

This is what the files look like in the folder.

Prefix suffix PowerShell replace text

Remove characters from files in folder

To remove characters from all files in a folder, run the below commands.

In our example, we want to remove the characters – Local.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder) | Rename-Item -NewName {($_.Name).Replace(" - Local","")}

Remove double extensions from files in folder

To remove double extensions from files in a folder, run the below commands.

$folder = "C:\Temp\Documents"

(Get-ChildItem -File $folder) | Where-Object {$_.Extension -and $_.BaseName -like "*$($_.Extension)"} | Rename-Item -NewName {$_.BaseName}

This is what the files look like with double extensions.

Double extensions in File Explorer

This is what the files look like after removing the double extensions.

PowerShell remove double extensions

That’s it!

Read more: PowerShell remove quotation marks from a text file »

Conclusion

You learned how to add a prefix and suffix to all files in a folder with PowerShell. There are many possibilities to add, remove and replace file names with PowerShell. The Rename-Item cmdlet is excellent for such tasks.

Did you enjoy this article? You may also like Excel CSV triple quotes when saving file. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 8 Comments

  1. Hi Ali
    Would be so grateful for your help.
    Am very low level at any form of coding – mostly cut and paste.
    I must be doing something wrong.

    Am trying to add a common suffix to multiple files. Files are place names and want to add counties and country – e.g. -Berkshire-UK
    I tried the solution above and copied below changing the folder name to “f:\images\locations” and changing “- Cloud” to the county name but nothing seems to happen:
    $folder = “C:\Temp\Documents”

    (Get-ChildItem -File $folder) | Rename-Item -NewName {$_.BaseName + ” – Cloud” + $_.Extension}

    Thank you for any advice.

  2. Hello,
    Thanks for this tutorial, it’s quite helpful. I’m having an unusual problem with one aspect. Trying to Rename files by appending the name of the Directory which is holding them, using:

    Get-ChildItem -Recurse | Where-Object {$_.Psiscontainer -eq $false} | Rename-Item -NewName {$_.Directory.Name + "_" + $_.Name}

    It works perfectly for the first nine folders, then on the tenth, starts throwing error messages (cannot find the path). In some cases, it creates file names with the Directory name written over and over again. Any idea what might be causing this?

  3. Hi!

    How can I add a prefix to a file based on a part of the filename?

    Example: “sampletext.01.pdf” rename to “01-sampletext.01.pdf”

    This would be based on the “01” at the end of the file. I have multiple files to rename every month so it should be able to do in batch (01 / 02 / 03 etc).

    so the next filename may be completely different, only the final 2 characters will be the same (01 / 02 / 03 etc). e.g. “aaabbbxgdf.02.pdf” and “gge83hoceiie.03.pdf”

    Thanks in advance.

    1. This will add a prefix for all the files in the folder:

      $folder = "C:\Temp\Documents"
      
      Get-ChildItem -File $folder | ForEach-Object {
          $newName = '{0}-{1}' -f $_.BaseName.Substring($_.BaseName.Length - 2), $_.Name
          $_ | Rename-Item -NewName $newName
      }

      This will only add a prefix for the PDF files in the folder:

      $folder = "C:\Temp\Documents"
      
      Get-ChildItem -File $folder -Filter "*.pdf" | ForEach-Object {
          $newName = '{0}-{1}' -f $_.BaseName.Substring($_.BaseName.Length - 2), $_.Name
          $_ | Rename-Item -NewName $newName
      }

Leave a Reply

Your email address will not be published. Required fields are marked *