Failstyle Failstyle - 3 months ago 16
PowerShell Question

Powershell Rename-Item Multiple In to 1 out and case sensitivity

The following code works beautifully:

Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse | Rename-Item -NewName { $_.Name.replace("DVDRIP","DVD") }


Now, 2 questions:


  1. Anyway I can input multiple words to replace to DVD? Here is what I was thinking:

    Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse | Rename-Item -NewName { $_.Name.replace("DVDRIP|DVDR|DVD5|DVD9","DVD") }


    I have tried this and other variations, but I just can't figure it out.

  2. How can I make the code NOT case sensitive so I don't have multiple lines trying to catch the possible different spellings?


Answer

You are using a regex-pattern with a normal text-replace method. Try -replace which is made for regex. -replace is also case-insensitive by default.

Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse |
Rename-Item -NewName { $_.Name -replace "DVDRIP|DVDR|DVD5|DVD9","DVD" }