To rename multiple files using PowerShell, you can use the Rename-Item
cmdlet. This cmdlet allows you to specify the path to the file or files you want to rename, and the new name for the file or files.
For example, suppose you have a directory with the following files:
1 2 3 |
file1.txt file2.txt file3.txt |
To rename all of these files so that they have the extension .bak
, you can use the following command:
1 |
Get-ChildItem *.txt | Rename-Item -NewName {$_.name -replace '.txt','.bak'} |
This command uses the Get-ChildItem
cmdlet to get a list of all files with the .txt
extension, and pipes the results to the Rename-Item
cmdlet. The -NewName
parameter specifies the new name for the files, which is constructed by replacing the .txt
extension with .bak
.
You can also use the Rename-Item
cmdlet to perform more complex renaming operations. For example, you can use regular expressions to match specific patterns in the filenames, and use backreferences to include parts of the original filename in the new name.
For more information about the Rename-Item
cmdlet and how to use it, you can type Get-Help Rename-Item
at the PowerShell prompt to read the documentation for the cmdlet.