0

I'm sorry to post this, I don't have the reputation to make the question on the comments of the post. So i read this in a thread (see link below) on how to rename files with Power Shell. My particular case was to remove character at the end.

(BTW i'm not familiar with PowerShell, so have mercy on me for the simple question).

How do I remove the same part of a file name for many files in Windows 7?

I use it the first time and worked perfectly fine. The second time around I got the following Error message:

Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'NewName'. Specified
method is not supported.
At line:1 char:69
+ ... df | ForEach-Object { Rename-Item $_$_.name.replace("-Layout1", "") }
+                                                        ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameItemCommand

I don't understand what is wrong since the first time worked. Can someone guide me please?

Thank guys for making this website great!

  • 2
    Missing space between $_ and $_.name.replace("-Layout1", "") – JosefZ Oct 26 '18 at 21:39
2

You should at least post the complete failing command.

From the error message I see there is a space missing between the two $_$_

Should be

... | ForEach-Object { Rename-Item $_ $_.Name.replace("-Layout1", "") }
  • good catch on the missing space. I was wondering why "ForEach-Object" in the error message is not simply foreach like the linked question about renaming MP3s. Perhaps the error message is reformatting the original command? Posting the command used verbatim would be very helpful to debugging it, great suggestion. – Blaelph Oct 26 '18 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.