I'm struggling to write a Powershell Command that does the following. Assume A folder which has a bunch of files with random names that match a regex pattern. I would like to capture the part that matches the pattern and rename the file to that part only.
E.g. "asdjlk-c12aa13-.pdf" should become "c12aa13.pdf" if the pattern is \w\d+\w+\d+
(or similiar).
My current idea looks something like this:
Get-ChildItem | Rename-Item -NewName { $_.Name -match $pattern ... } -WhatIf
where ...
needs to be replaced with something that sets the "value" of the codeblock (i.e. the NewName) to the matched group. I.e. I don't know how to access $matched
directly after the -match
command.
Also, I wonder if it's possible to do lazy matching using -match
, .*?
doesn't seem to do the trick.