Why do we need to use the import CSV (comma-separated values) delimiter in PowerShell? By…
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.
Table of contents
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.
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.
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.
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.
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.
This is what the files look like after removing the 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.
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.
What you’re doing is correct, and it should work (I just tested it).
Thank you sincerely for the excellent article/tutorial. Your work is greatly appreciated.
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:
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?
Your command is correct and works fine when I test it with 10+ folders.
Excellent article / tutorial. Thank you so much.
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.
This will add a prefix for all the files in the folder:
This will only add a prefix for the PDF files in the folder: