Logo

dev-resources.site

for different kinds of informations.

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

Published at
9/18/2024
Categories
webdev
cmd
powershell
microsoft
Author
dharamgfx
Categories
4 categories in total
webdev
open
cmd
open
powershell
open
microsoft
open
Author
9 person written this
dharamgfx
open
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! โšก

powershell Article's
30 articles in total
Favicon
Windows ไธŠ VSCode ็š„ C/C++ ๅปถไผธๆจก็ต„่™•็†็ทจ็ขผ็š„ๅ•้กŒ
Favicon
PowerShell ็š„ๆ–‡ๅญ—็ทจ็ขผ
Favicon
็ฌ”่ฎฐ3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
โ“ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! ๐Ÿš€
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: