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)
}
$hex_name[0]
to get the0x8f860be3
without the parens, or change your split to something like$firstParen, $hex_name, $secondParenAndExt = ($_.BaseName).Split('()')
($hex_name + "_0001" + $_.Extension).ToString()
and confirmed that does not work. docs.microsoft.com/en-us/dotnet/api/…. Even if you need to set as a variable within the loop and then use with theRename-Item
.... just a quick idea with no testing.