PowerShell - Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'NewName'

FatalBulletHit

I'm trying to bulk rename a bunch of files with PowerShell, however, I run into an error which I don't know what to do with and searching for it didn't give me any solutions either.

Error message:

Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'NewName'.


For example: 2068227794 (0x8f860be3).bnk to 0x8f860be3_0001.bnk

In this particular case $hex_name is 0x8f860be3.

$source = "K:\_test"

Get-ChildItem -Path $source -File -Recurse -Include "* (0x*).*" | ForEach-Object {

   $prefix, $hex_name = ($_.BaseName).Split('()')
   Rename-Item -Path $_ -NewName ($hex_name + "_0001" + $_.Extension)

}
FatalBulletHit

As this question was answered in the comments, I'll answer my own question so it is marked as solved.

The error occurs as $hex_name is an array which contains the hex string and the rest. There are 2 simple solutions:

Selecting the item directly:

$hex_name = ($_.BaseName).Split('()')[1]

Assigning the unwanted rest to a new variable:

$prefix, $hex_name, $rest = ($_.BaseName).Split('()')

All credit goes to EBGreen.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set-Location : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported

From Dev

cannot implicitly convert System.Type to object

From Dev

Cannot process argument transformation on parameter 'machine'. Cannot convert value to type System.String

From Dev

SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

From Dev

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

From Dev

Powershell - Cannot convert value of type "System.Management.Automation.PSCustomObject" to type "System.Management.Automation.PSCustomObject"

From Dev

Assert.IsInstanceOfType cannot convert from object to System.Type

From Dev

Cannot convert type 'object[]' to 'System.Data.DataRow'

From Dev

Assert.IsInstanceOfType cannot convert from object to System.Type

From Dev

Cannot implicity convert type 'object ' to system.collection.arraylist

From Dev

Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert to type string to System.Net.Mail.MailAddress

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

From Dev

Cannot implicitly convert type string to System.Drawing.Color

From Dev

Cannot Bind Argument to Parameter 'NewName' because it is an empty string

From Dev

Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

From Dev

Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

From Dev

Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Web.Mvc.SelectList'

From Dev

Cannot implicity convert type string to System.Func<string,int,string,string>

From Dev

Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

From Dev

ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

Related Related

  1. 1

    Set-Location : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported

  2. 2

    cannot implicitly convert System.Type to object

  3. 3

    Cannot process argument transformation on parameter 'machine'. Cannot convert value to type System.String

  4. 4

    SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

  5. 5

    Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

  6. 6

    Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

  7. 7

    Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

  8. 8

    Powershell - Cannot convert value of type "System.Management.Automation.PSCustomObject" to type "System.Management.Automation.PSCustomObject"

  9. 9

    Assert.IsInstanceOfType cannot convert from object to System.Type

  10. 10

    Cannot convert type 'object[]' to 'System.Data.DataRow'

  11. 11

    Assert.IsInstanceOfType cannot convert from object to System.Type

  12. 12

    Cannot implicity convert type 'object ' to system.collection.arraylist

  13. 13

    Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

  14. 14

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  15. 15

    Cannot implicitly convert to type string to System.Net.Mail.MailAddress

  16. 16

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  17. 17

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  18. 18

    Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

  19. 19

    Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

  20. 20

    Cannot implicitly convert type string to System.Drawing.Color

  21. 21

    Cannot Bind Argument to Parameter 'NewName' because it is an empty string

  22. 22

    Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

  23. 23

    Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

  24. 24

    Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Web.Mvc.SelectList'

  25. 25

    Cannot implicity convert type string to System.Func<string,int,string,string>

  26. 26

    Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]'

  27. 27

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  28. 28

    ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

  29. 29

    ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

HotTag

Archive