none
How to Rename a files in powershell if the files has characters [ ] RRS feed

  • Question

  • I try rename the files with the next Script , for delete the characters [ and ], but recognized as wildcards:

    $cad = read-host "Please enter your name:"

    Get-ChildItem -Recurse -Filter "*$cad*" | Rename-Item -NewName {$_.name -replace $cad,""}

    The files has the next pattern:

    File 1[machine1].xls

    Filewithanotherdata[machine3].docx

    OtherFile[mac].pdf

    • Moved by Bill_Stewart Monday, July 29, 2019 8:22 PM Abandoned
    Wednesday, May 9, 2018 1:03 PM

All replies

  • Does it work if you escape the characters with the backtick, "`" escape character?

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, May 9, 2018 1:18 PM
  • Also, notice that you need to run your code 3 times to rename the 3 file examples you listed, as each has a different string between the [] characters. If you search on "[*]" you will replace the braces and the strings between the braces with blanks. You probably should run the script twice, once for "[" and then again for "]", so you only remove the braces (if that is your intent).

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, May 9, 2018 4:00 PM
  • No

    With the backtick ` its ok the command

    Get-ChildItem -Recurse -Filter "*`[*"

    But if I use the next sentence, the error is with the regular expression

    Get-ChildItem -Recurse -Filter "*`[*" | Rename-Item -NewName {$_.name -replace '`[',''} -WhatIf

    This is the error:

    Rename-Item : Error en la entrada al bloque de script para el parámetro 'NewName'. El patrón de expresión regular `[ no es válido.
    En línea: 1 Carácter: 62
    + ... lter "*`[*" | Rename-Item -NewName {$_.name -replace '`[',''} -WhatIf

    Wednesday, May 9, 2018 4:35 PM
  • No

    With the backtick ` its ok the command

    Get-ChildItem -Recurse -Filter "*`[*"

    But if I use the next sentence, the error is with the regular expression

    Get-ChildItem -Recurse -Filter "*`[*" | Rename-Item -NewName {$_.name -replace '`[',''} -WhatIf

    This is the error:

    Rename-Item : Error en la entrada al bloque de script para el parámetro 'NewName'. El patrón de expresión regular `[ no es válido.
    En línea: 1 Carácter: 62
    + ... lter "*`[*" | Rename-Item -NewName {$_.name -replace '`[',''} -WhatIf

    Wednesday, May 9, 2018 4:53 PM
  • Regular expressions, used by the -Replace parameter, use the backslash, "\", as the escape character. See this documentation:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-6

    which says:

    Notice that the escape character for regular expressions, a backslash (\), is different from the escape character for Windows PowerShell. The escape character for Windows PowerShell is the backtick character (`) (ASCII 96).


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Wednesday, May 9, 2018 8:26 PM
    • Unproposed as answer by Bill_Stewart Monday, July 29, 2019 8:22 PM
    Wednesday, May 9, 2018 5:48 PM
  • The backslash even resolver the problem :(

    Wednesday, May 9, 2018 10:30 PM