Powershell Rename-Item replace multiple naming parameters in one pass

721 Views Asked by At

I would like to replace a few filename parameters in one pass. Trying the following results in errors claiming the 'file already exists'. Is there a way to name multiple replace parameters in a single pass?

file | Rename-Item -NewName { $_.Name -replace "parameter1", "X" , $_.Name -replace "parameter2", "Y" }
1

There are 1 best solutions below

0
On BEST ANSWER

As requested, my comment as answer.

You can daisy-chain -replace, so in this case

$_.Name -replace "parameter1", "X" -replace "parameter2", "Y"

Sorry for the short answer, but I'm on mobile now..