skip to Main 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 – Cloud.

$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 0 Comments

Leave a Reply

Your email address will not be published.