Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Rename-Item: A positional parameter error

$
0
0

I am tring to write a quick interactive command that will rename all the jpgs in the current directory and came up with the following command:

$i=1; gci | ? { ($_.Extension -eq ".JPG") -and !($_.Name.StartsWith("cover")) } | % { Rename-Item -Path ".\$_" -NewName [string]::Format("picture_$i.

jpg"); $i++ }

as you can see, there are multiple commands strung together. The first command initialises a variable $i, this is a counter. The next command starts a pipeline with Get-ChildItem, next comes Where-Object (represented by ?), followed by ForEach-Object (represented by %). There is no problem up to this point. However, there appears to be a problem with the Rename-Item cmdlet:

Rename-Item : A positional parameter cannot be found that accepts argument 'picture_120.jpg'.
At line:1 char:98
+ $i=1; gci | ? { ($_.Extension -eq ".JPG") -and !($_.Name.StartsWith("cover")) } | % { Rename-Item <<<<  -Path ".\$_" -NewName [string]::Format("picture_$i.jp
g"); $i++ }
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameItemCommand


There appears to be a parameter binding issue. But the 2 parameters I have specified (-Path and -NewName) I have named so I assumed that position is not of relevance. What am I missing here?

Viewing all articles
Browse latest Browse all 6937

Trending Articles