Ok, you're on the right path with the whole RegEx tag here. What I did is look for everything until the last backslash, then captured 5 digits, then everything that isn't a backslash until the end of the line, and only returned the captured group. I set that as a variable $ItemNumber
, and used that in the destination. I ran that on a ForEach loop for everything in the target source folder. Here's the code I ended up with:
ForEach($File in (GCI "X:\Property Files\*.PDF")){
$ItemNumber = $File.Fullname -replace ".+?\\(\d{5})[^\\]*$", "`$1"
move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- $ItemNumber"
}
You could do it through the pipe if you wanted, like this:
GCI "X:\Property Files\*.PDF"|%{move-item -path X:\"Property Files"\05165*.pdf -destination X:\"Property Files"\"* -- $($_.Fullname -replace ".+?\\(\d{5})[^\\]*$", "`$1")"}
But that gets kind of long, and some people really don't like that long of a single line.
That RegEx can be tested here, plus it breaks it all down. (link to regex101.com explanation)