powershellrename-item-cmdlet

Powershell Rename-Item replace multiple naming parameters in one pass


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" }

Solution

  • 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..