Powershell special character text formatting in variable RRS feed

  • Question

  • Hi All,

    I'm sure this was already sorted out somehow somewhere but I couldn't find the answer within a veery long time.

    I'm currently working on script where the name of the object (and I must work with name) contains character '[' and ']' .

    The operation I'm doing with it is -match

    therefore I have:




    and I'm trying to compare this list with many different outputs such as


    The variables are just an example, i'm getting them as an output. What I need actually to set properly is following line.

    $OutputList -match $Comparision

    The error I'm getting:

    parsing "Example[g-CN_LIP]" - [x-y] range in reverse order.

    At line:10 char:1

    + $OutputList -match $Comparision

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : OperationStopped: (:) [], ArgumentException

        + FullyQualifiedErrorId : System.ArgumentException

    The problem is that the $Comparision contains those "[" and "]" characters and I can't manage to explain powershell


    Please help me out. I tried to replace, no success. I tried regex, it added character "\" to every space, [ and ] therefore I can't compare it anymore because the text is different.

    I need to be able to use

    • Edited by Poistenec Tuesday, February 13, 2018 10:00 AM
    • Moved by Bill_Stewart Friday, March 9, 2018 7:57 PM Abandoned
    Tuesday, February 13, 2018 9:52 AM

All replies

  • 'Example[1]2"' -match '\[1\]'


    • Proposed as answer by jrv Tuesday, February 13, 2018 10:08 AM
    Tuesday, February 13, 2018 10:01 AM
  • Hi poistenec,

    I tried this code on my powershell (Windows 10, Powershell 5) and it works fine. 

    $OutputList = "Example1



    $Comparision = "Example[1]"

    $OutputList -match $Comparision



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. Regards Simon Disclaimer: This posting is provided AS IS with no warranties or guarantees, and confers no rights.

    Tuesday, February 13, 2018 10:01 AM
  • I am sorry, I wrote wrong example on the beginning, please try edited one.

    Sorry for confusing you guys

    Tuesday, February 13, 2018 10:06 AM
  • Help about_Regular_Expressions


    Tuesday, February 13, 2018 10:09 AM
  • Unfortunately I wasn't able to solve this issue. I tried everything but -match operation was much too strict.

    Probably there is a way to sort this out but I couldn't find any and I don't want to study powershell from it's roots again just to solve this simple issue.

    I did a work around to delete everything after "[" as the remaining data were unique enough.

    This might help someone in future maybe.

    $BracelessComparision = $Comparision -replace "\[.*"

    (replace everything after sign "[")

    Thanks anyways

    Tuesday, February 13, 2018 1:26 PM
  • The issue has nothing to do with PowerShell.  I is about regular expressions. I gave you the answer twice but you ignored it.

    See: https://en.wikipedia.org/wiki/Regular_expression


    Tuesday, February 13, 2018 1:37 PM