locked
How to use strings in PowerShell RRS feed

  • Question

  • Is there a way to handle strings that contain BOTH single quotes and double quotes ? 

    for example: Kjw'NhertyH)Blt0wFR|u/6mKn<}GH"

    How do I pass that as a Parameter value to a PowerShell script and ensure that it doesn't get padded by escape characters that need to be handled. 

    Appreciate any help !

    • Split by jrv Wednesday, May 20, 2020 5:13 AM New topic
    Wednesday, May 20, 2020 4:54 AM

All replies

  • Place the string in single quotes a double the embedded single quotes.

    'Kjw''NhertyH)Blt0wFR|u/6mKn<}GH"'

    A single quote escapes an embedded single quote.


    \_(ツ)_/

    • Proposed as answer by Vector BCO Wednesday, May 20, 2020 7:13 PM
    Wednesday, May 20, 2020 5:22 AM
  • Place the string in single quotes a double the embedded single quotes.

    'Kjw''NhertyH)Blt0wFR|u/6mKn<}GH"'

    A single quote escapes an embedded single quote.


    \_(ツ)_/

    Hi objectNotFound, as usual you have some options with some benefits and some restrictions

    first thing proposed by jrv  can be helpful for short lines, or lines that you providing from the keyboard,

    other way that you can do similar things without string modification is using next construction

    $string = @'
    Kjw'NhertyH)Blt0wFR|u/6mKn<}GH"
    '@

    where you can store multi line text if you need as a single line

    At the same time if you receiving this sting from external command (file or whatever) you do not need any additional actions

    $string = Get-Content c:\string\with2typeQuotes.txt


    The opinion expressed by me is not an official position of Microsoft

    • Proposed as answer by cguan Wednesday, May 20, 2020 9:21 AM
    Wednesday, May 20, 2020 8:36 AM