DEV Community

Cover image for Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! πŸš€
Dharmendra Kumar
Dharmendra Kumar

Posted on

3 1 1 1 1

Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! πŸš€

Renaming files manually can be exhausting, but with PowerShell, you can batch rename multiple files in just one click on Windows! In this post, we’ll walk you through a simple yet effective PowerShell script to rename files in sequence or based on specific conditions. Let’s break down the script and customize it for various use cases.

Image powershell


The Script:

Get-ChildItem *.png | ForEach-Object {
    $newName = $_.BaseName.TrimStart('0') + $_.Extension
    Rename-Item $_.FullName -NewName $newName
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Line-by-Line Breakdown:


1. Get-ChildItem *.png

  • What it does: Retrieves all .png files in the current directory for processing.

Example:

Get-ChildItem *.jpg  # To fetch all JPEG files
Enter fullscreen mode Exit fullscreen mode

2. | ForEach-Object { ... }

  • What it does: Loops through each file and applies the actions inside the curly braces { ... }.

Example:

Get-ChildItem *.txt | ForEach-Object { 
    Write-Host $_.Name 
}
Enter fullscreen mode Exit fullscreen mode

3. $_.BaseName.TrimStart('0') + $_.Extension

  • What it does: Removes leading zeroes from the file name while keeping the file extension intact.

Example:

$newName = $_.BaseName.TrimStart('A') + $_.Extension  # Removes leading 'A' characters
Enter fullscreen mode Exit fullscreen mode

4. Rename-Item $_.FullName -NewName $newName

  • What it does: Renames the file using its new name generated in the previous step.

Example:

Rename-Item $_.FullName -NewName "newFile_" + $_.Name  # Add 'newFile_' prefix
Enter fullscreen mode Exit fullscreen mode

🎯 Different Scenarios and Examples

1. Renaming Files by Adding a Suffix

   Get-ChildItem *.txt | ForEach-Object {
       $newName = $_.BaseName + '_backup' + $_.Extension
       Rename-Item $_.FullName -NewName $newName
   }
Enter fullscreen mode Exit fullscreen mode

This script renames file.txt to file_backup.txt.

2. Changing File Extensions

   Get-ChildItem *.png | ForEach-Object {
       $newName = $_.BaseName + '.jpg'
       Rename-Item $_.FullName -NewName $newName
   }
Enter fullscreen mode Exit fullscreen mode

This renames image1.png to image1.jpg.

3. Adding Sequential Numbers to Files

   $counter = 1
   Get-ChildItem *.png | ForEach-Object {
       $newName = "image$counter" + $_.Extension
       Rename-Item $_.FullName -NewName $newName
       $counter++
   }
Enter fullscreen mode Exit fullscreen mode

This renames files sequentially like image1.png, image2.png, etc.


πŸ’‘ Conclusion

Using PowerShell in Windows, you can easily rename multiple files in sequence with just one click! Whether you’re stripping zeroes, adding suffixes, or changing file extensions, this script is a flexible and powerful tool to streamline your file renaming tasks.

Happy scripting! ⚑

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay