Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell file renaming by filtering on more than one condition

I've got a bunch of files in the following format:

00092-100221-01M-V-3-20-001 Building A, plan 1M Piping

I'd like to rename only the files containing both the words "Building A" and "Piping" I've tried the following code

dir -filter "*Building A*", "*Piping*" | Rename-Item -NewName {$_.basename + " As built" + $_.extension}

but I get an error stating that the parameter -filter does not support this method. Is there a way I can filter for 2 conditions in Powershell?

like image 511
Romaine Veggum Avatar asked Oct 15 '25 05:10

Romaine Veggum


1 Answers

The -Filter parameter accepts only one argument. You could use the -Include parameter but this can be tricky (look at the docs).

The easiest way to filter pipeline elements in general is the Where-Object cmdlet (short where):

dir | where { $_.Name -match "Building A" -and $_.Name -match "Piping" } | Rename-Item ...

Note: The -match operator uses regex matching, which is fine in this case. You could also use the -like operator with wildcards: $_.Name -like "*Piping*"

like image 152
marsze Avatar answered Oct 17 '25 16:10

marsze