Another approach, using regular expressions:
Get-ChildItem *.pdf | % {
$newName = $_.Name -replace '^(.*) (\S+) (\S+\.pdf)$', '$2 $1 $3'
Rename-Item $_ $newName
}
Regular expression breakdown:
^(.*)
matches any number of characters at the beginning of the string and captures them in a group.[Date]
in your example filename[Date] [Lastname] 345.pdf
.(\S+)
matches one or more consecutive non-whitespace characters and captures them in a second group.[Lastname]
in your example filename[Date] [Lastname] 345.pdf
.(\S+\.pdf)$
matches one or more consecutive non-whitespace characters followed by the string.pdf
at the end of the string and captures that in a third group.345.pdf
in your example filename[Date] [Lastname] 345.pdf
.
The groups are referred to by $1
, $2
and $3
, so the replacement string '$2 $1 $3'
re-orders the groups the way you want.