You really need to use this forum's Code Block tool when posting Powershell code. Otherwise, certain characters get treated as formatting characters. That's why your post looks strange.
I tried to recreate your folder/file structure with these command prompt statements. I don't know if this is correct or not.
cd \temp
rd /q /s RenameTest
md RenameTest
cd RenameTest
md Input
md Output
cd Input
md "Media file 1 (2000) [1080p] [BluRay] [5.1] [YTS.MX]"
cd "Media file 1 (2000) [1080p] [BluRay] [5.1] [YTS.MX]"
echo "xxxx" > "Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].mp4"
echo "xxxx" > "Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].srt"
cd ..
md "Media file 2 (2000) [720p] [WEBRip] [YTS.MX]"
cd "Media file 2 (2000) [720p] [WEBRip] [YTS.MX]"
echo "xxxx" > "Media.File.2.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4"
cd \temp\renametest
dir /b /s
I then tweaked your script to get it to work. One problem appears to be that the cleanName variable is not unique enough when all files are getting stored in the same destination folder. I added code to look for an existing file and then add a counter to the name to make it unique.
I don't know if that's what you want or not.
#***********
#Variables *
#***********
$newLocation = "C:\Temp\RenameTest\Output"
$path = "C:\Temp\RenameTest\Input"
#*****************************************************************************************
#Locate the media files in the specified folder (this folder will never change location) *
#*****************************************************************************************
$mediaFile = Get-ChildItem -LiteralPath "$path" -Recurse | Where-Object {$_.extension -match ".srt" -or $_.extension -match ".mkv" -or $_.extension -match ".mp4"}
foreach ($media in $mediaFile) {
"---------------------------------------------------"
"Media.fullname = {0}" -f $media.fullname
""
#************************************************************************************************
#Remove any periods from the file name and clean up any extra junk at the end of the file name *
#************************************************************************************************
$fullFileName = $media.baseName.replace("."," ").replace("(","")
"fullFileName = {0}" -f $fullFileName
$cleanName = $fullFileName -replace '\s*\d.*'
"cleanName = {0}" -f $cleanName
#*******************************************
#Find the four digit year in the file name *
#*******************************************
$year = ([Regex]::Matches($fullFileName, '(19|20)[\d]{2,2}')).value
#**********************************************************************************************
#Generate a new file name following the standard format of [Movie Name] ([Year]).[extension] *
#**********************************************************************************************
$newName = "$cleanName ($year)$($media.extension)"
"newName = {0}" -f $newName
$newNamePlusDir = "$newLocation\$newname"
"newNamePlusDir = {0}" -f $newNamePlusDir
$movedFileName = "{0}\{1}" -f $newLocation, $media.Name
"Moved name = {0}" -f $movedFileName
# Look to see if a file with the new name already exists in the destination folder
$count = 1
while (Test-Path $newNamePlusDir) {
"A file with the newname already exists in the output folder. "
$newName = "$cleanName ($year)($count)$($media.extension)"
"newName = {0}" -f $newName
$newNamePlusDir = "$newLocation\$newname"
"newNamePlusDir = {0}" -f $newNamePlusDir
$count++
}
#******************************************
# Move the movie file into the new folder *
#******************************************
Move-Item -LiteralPath $media.fullName -Destination $newLocation
#******************************************
# Now rename it *
#******************************************
Rename-Item -LiteralPath $movedFileName -NewName $newName
""
}