PowerShell Rename File Ext.
<#
ScriptsbyScott.com
Special Note: Backup your files and do Testing
.SYNOPSIS
Rename File Extensions
.DESCRIPTION
Get list of files by exteneion type from folder
Renames files with new extesnion you provide
Works with file ext of any length .txt .html .doc .docx .shtml etc...
.NOTES
I Changed my website from HTML to PHP
and needed to change file extensions and
I like to make my own PowerShell tools :)
Requires Rights on Folder Path
Author: Scott Head
Min PSVer: Powershell 2.0
Version: 1.0 (7/7/2019) Script Created
#>
Try{
#============================================
# Enter folder path and File Ext Typer
#============================================
write-host ""
$MyFolderPath = Read-Host "Enter Folder Path"
write-host ""
$Ext = Read-Host "Enter File Extention like .txt"
write-host ""
$NewExt= Read-Host "Enter New File Extension like .php"
#============================================
# Get List of Files from Folder
#============================================
$Myfile = Get-ChildItem $MyFolderPath -ErrorAction Stop
#============================================
# Get List of Files only with Extension .html
#============================================
$MyfileNew=@()
$MyfileNew= $Myfile | Where{$_.Name -like "*$Ext"} -ErrorAction Stop
#=======================================================
# Displays list of files to be altered and Logs to File
#=======================================================
CLS
# set the new color
$host.UI.RawUI.ForegroundColor = "Green"
Write-Output "Files to be Chankged: `n"
Get-Date | Out-File C:\temp\FilesChanged.txt -Append
$MyfileNew.Name | Tee-Object C:\temp\FilesChanged.txt -Append
Write-Output ""
Pause
# set the new color
$host.UI.RawUI.ForegroundColor = "white"
#============================================
# Loop Through All File Names
#============================================
Foreach($FileName in $MyfileNew.Name){
#=============================================================
# Trim file removing Last 5 char (.html) and Appending .php
#============================================================
$NewFileName=$FileName.Substring(0,$FileName.Length-$Ext.Length) + "$NewExt"
#=================
# Renames Files
#=================
Rename-item "$MyFolderPath\$FileName" "$MyFolderPath\$NewFileName" -ErrorAction Stop
}
}Catch{
Write-Output $_.Exception
}