Mohit Singh
Mohit Singh

Reputation: 153

I am unable to change the extension of a multiple files using the powershell

To change the extension of files located at: C:\Users\mohit singh\Desktop\spotlight, I typed the following command in the PowerShell

C:\Users\mohit singh\Desktop\spotlight> ren *.* *.jpg

But I get the following error:

ren : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path"
argument and run the operation again.
At line:1 char:1
+ ren *.* *.jpg
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

Upvotes: 3

Views: 2523

Answers (1)

msanford
msanford

Reputation: 12227

You're trying to use the ren command from within PowerShell. However, ren in Powershell is an alias to Rename-Item which is a different command. Powershell and cmd are different shells that use different command interpreters.

Your simplest option is just to run your command from a cmd window, instead of Powershell.

But if you wanted to use Powershell, you can do this by getting every file in the current folder, piping it to Rename-Item, then using the ChangeExtension .Net API to change its extension (which is safer than simple string replacement).

Get-ChildItem -File | Rename-Item -NewName { [io.path]::ChangeExtension($_.Name, "jpg") }

If you want to act on subfolders too, add -Recurse to Get-ChildItem.

Upvotes: 8

Related Questions