Executing a certain command for files in multiple subfolders at once in windows powershell

Executing a certain command for files in multiple subfolders at once in windows powershell

I have a folder with multiple subfolders containing PDF files which I am renaming by executing this command in each of the subfolder;

get-childitem *.pdf | foreach { rename-item $_ $_.Name.Replace("certainword", "") }

So is there any way to rename all PDF files in each of the subfolder at once in one command?

答案1

As already stated in the comments, you should use -Recurse parameter to apply the cmdlet to subfolders.

A small change to you cmdlet is that you don't need ForEach. You can pipe the files' list directly to Rename-Item, then use -NewName parameter.

Get-ChildItem -Recurse *.pdf | Rename-Item -NewName { $_.Name -replace 'certainword', '' }

相关内容