Powershell: Renaming TXT Files with a specific Pattern using Powershell

How to rename targeted files within a folder based on a pattern

Photo by Markus Spiske on Unsplash

Powershell is a powerful tool that allows you to do whatever you think of while managing the files on your computer in an efficient manner. Especially if you have a great number of files (in my case, pictures) besides knowing IT work.

My name is Jose Ortega, and I’m a Microsoft Certified Professional on Office 365, Powershell, Messaging (Exchange Online/Exchange On-premises), and Windows Server. I’ve been working with Microsoft Technologies for 12+ years and independently since 2015 using Upwork and reaching the distinction of Top-Rated Plus Freelancer. I used to write scripts for TechNet in my free time, but now I’ve moved to Medium; my goal is to help you solve complicated and straightforward problems with my experience. You can expect an article from one to three weeks. Right now, I’m working and assisting on a Microsoft GIG as a freelancer for Office365 tickets and customers worldwide. Thank you in advance for all the support, follow, clap, comment, and donation. If you would like to ask something or discuss it further, please reach me on social networks (Twitter, Instagram, Facebook, or GitHub as j0rt3g4).

My photography, I can be found on: jgocfoto, and it’s on Instagram, Facebook, Instagram, Youtube, 500px, 100asa, youpic, pexels, unsplash, adobe stock, now let’s go back to the requirement.

The requirement:

I have several files within a folder, and I would like to rename only the TXT files contained in that particular path and ignore any other different extension; I’d like to rename in a pattern like a 3 digit code:

  • Dummy name 1.txt
  • Dummy name 50.txt
  • Dummy name 99.txt
  • Dummy Name 300.txt

The Solution: In case that you like to copy-paste the procedure:

$i=0;
$nameLike='dummy name'
[int]$currentid=0
#RENAME TO REMOVE THE SPACE IN THE MIDDLE
gci -File -Path "./" | where{ $_.name -like "$nameLike*"} | %{
#remove space in the middle
$newname = $_.Name.Split(‘ ‘)[0] + $_.Name.Split(‘ ‘)[1]
#convert the name into the padding you want (3)
[int]::TryParse($_.Name.Split(' ')[2].Split('.')[0],[ref]$CurrentId) | Out-Null
$newname = $newname + $CurrentId.ToString("000") + $_.Extension
Rename-Item -Path $_.FullName -NewName $newname
}

In case you have doubts about how it should be looks like, I’ll let you a screenshot of the code:

Solution On Powershell ISE.

Original Files — Before Running the script

Original files before running the script

Output — After running the script

Output Files after running the script

Medium 👏 NETWORKING 🔗 SECTION 🏰

Photo by Helena Lopes on Unsplash

If you liked this article or require help to reach 100 subscriptions, drop a note in my previous article; if you don’t get an answer or Follow back, write me; my contacts are below.

Let’s quick thinking that I’m not doing Following you thinking, and I will follow you and read you when I have a Lil time this weekend let’s not feed the anger or hate , let’s think that the people are trying to live their lives the best they can with the knowledge they have at the moment.

Leave a comment/Follow me here (✅ to be — Added✅):

Or follow :) I’d add you if I see that your follower’s number is less than 100 and if you already have started writing at least one history!

Also, on this networking topic, I’ve created a challenge on my page:

The things are simple, go to the page and add yourself, then move to the “Randomize Page” and get a list of 3 to 6 people to follow that were already registered, this was an effort to help people to get more than 100 viewers fast, but sadly nobody has even written there, which is a little sad! At least it is there at disposition.

Thank you very much for reading. Any comments you can contact me over social networks (Twitter, Instagram, Facebook, or GitHub as j0rt3g4), or over teams/email if you prefer jortega@faboit.com. I want to know from you, leave your comments or doubts and share this information if you think it was helpful in any way for you.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store