I'm trying to do the following:
Rename-Item c:\misc\*.xml *.tmp
I basically want to change the extension on every files within a directory to .tmp
instead of .xml
. I can't seem to find a straight forward way to do this in PowerShell.
To rename and move an item, use Move-Item . You can't use wildcard characters in the value of the NewName parameter. To specify a name for multiple files, use the Replace operator in a regular expression.
Using the last example, you can use PowerShell to rename all the files so that the first word of the file name is "Our" instead of "My". Select the Start menu, type Powershell, and select Windows PowerShell to open the app.
Modify the file names of a large number of files (without giving all of the files the same name). Perform a search and replace on a targeted section of file names. Perform a regular expression rename on multiple files. Check expected rename results in a preview window before finalizing a bulk rename. Undo a rename operation after it is completed.
Instructs Windows PowerShell to take each item (object) found with the first command ( Get-ChildItem) and pass it to the second command ( Rename-Item) Tells Rename-Item to find the string “current” in the file name and replace it with “old”.
Batch Rename Files in Windows 10 Using File Explorer Open File Explorer and navigate to the folder that contains all the files you want to rename. Select View > Details in the Layout group in the ribbon. This lets you view the entire file name for each file in the... Select all files in the folder ...
From example 4 in the help documentation of Rename-Item
retrieved with the command:
get-help Rename-Item -examples
Example:
Get-ChildItem *.txt| Rename-Item -NewName { $_.Name -replace '\.txt','.log' }
Note the explanation in the help documentation for the escaping backslash in the replace command due to it using regular expressions to find the text to replace.
To ensure the regex -replace
operator matches only an extension at the end of the string, include the regex end-of-string character $
.
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '\.txt$','.log' }
This takes care of the case mentioned by @OhadSchneider in the comments, where we might have a file named lorem.txt.txt
and we want to end up with lorem.txt.log
rather than lorem.log.log
.
Now that the regex is sufficiently tightly targeted, and inspired by @etoxin's answer, we could make the command more usable as follows:
Get-ChildItem | Rename-Item -NewName { $_.Name -replace '\.txt$','.log' }
That is, there is no need to filter before the pipe if our regex sufficiently filters after the pipe. And altering the command string (e.g. if you copy the above command and now want to use it to change the extension of '.xml' files) is no longer required in two places.
This works well too when you're in the desired directory.
Dir | Rename-Item –NewName { $_.name –replace "old","new" }
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