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?
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*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With