Skip to content

Instantly share code, notes, and snippets.

@lrckt
Created April 8, 2025 01:44
Show Gist options
  • Save lrckt/532cc393432be5a15cbe913e39b7e3bc to your computer and use it in GitHub Desktop.
Save lrckt/532cc393432be5a15cbe913e39b7e3bc to your computer and use it in GitHub Desktop.
# Set the path to the folder containing the files
$folderPath = "C:\Path\To\Your\Folder"
# Get all files in the folder
Get-ChildItem -Path $folderPath -File | ForEach-Object {
$file = $_
# Use regex to match the pattern ending with _numbers (e.g. _20230230230)
if ($file.Name -match "^(.*?\.\w+)_\d+$") {
$newName = $matches[1]
# Full path for new name
$newFullPath = Join-Path -Path $folderPath -ChildPath $newName
# Rename the file
Rename-Item -Path $file.FullName -NewName $newFullPath
Write-Host "Renamed '$($file.Name)' to '$newName'"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment