Powershell script runs to add letter M in front of all file names. Then it continues to loop after the last file is modified and adds more MMMMs in front of the file name, in a loop. I need it to stop after the first run through all files in Directory.
I tried these three versions. The first with the space in front of M renames all files once but keeps the extra space in front of the name, if I can get rid of the space, it would be what I need:
DIR | Rename-Item -NewName {" M" + $_.BaseName + $_.Extension}
This version does not have the extra space in front of the M, but keeps adding MMMMs as long as the file name permits more characters.
DIR | Rename-Item -NewName {"M" + $_.BaseName + $_.Extension}
I also tried putting the letter as a parameter and using Foreach and got the same results as with just letter M in the first two versions:
$AL = "M"
Foreach-Object {
DIR | Rename-Item -NewName {$AL + $_.BaseName + $_.Extension}
}
I need the files renamed with an M in front of the name, once and without the extra space. Thank you.
Mto the start of the file name unlessMis already the first character?DIR | ForEach-Object {Rename-Item -NewName ('M' + $_.BaseName + $_.Extension)}PathtoRename-Item.DIR | ForEach-Object {Rename-Item -Path $_ -NewName ('M' + $_.BaseName + $_.Extension)}does work, however.$_.BaseName + $_.Extensioncan be replaced by$_.Name.