Expand variable and add results in single quotes









up vote
1
down vote

favorite
1












Silly question, but cant seem to figure it out.
How do I expand the content of a variable, and show the result in single quotes ?



 $Test = Hello
Write-output $($Test)


I would like the result to be 'Hello' including the quotes.










share|improve this question





















  • Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
    – Jacob
    Nov 10 at 17:37











  • Thanks for quick reply :)
    – user7490700
    Nov 10 at 17:40










  • if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
    – Lee_Dailey
    Nov 10 at 17:46










  • Had to use it like this $("'$Test'") , but you pointed me in the right direction
    – user7490700
    Nov 10 at 17:49










  • I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
    – user7490700
    Nov 10 at 17:52














up vote
1
down vote

favorite
1












Silly question, but cant seem to figure it out.
How do I expand the content of a variable, and show the result in single quotes ?



 $Test = Hello
Write-output $($Test)


I would like the result to be 'Hello' including the quotes.










share|improve this question





















  • Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
    – Jacob
    Nov 10 at 17:37











  • Thanks for quick reply :)
    – user7490700
    Nov 10 at 17:40










  • if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
    – Lee_Dailey
    Nov 10 at 17:46










  • Had to use it like this $("'$Test'") , but you pointed me in the right direction
    – user7490700
    Nov 10 at 17:49










  • I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
    – user7490700
    Nov 10 at 17:52












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Silly question, but cant seem to figure it out.
How do I expand the content of a variable, and show the result in single quotes ?



 $Test = Hello
Write-output $($Test)


I would like the result to be 'Hello' including the quotes.










share|improve this question













Silly question, but cant seem to figure it out.
How do I expand the content of a variable, and show the result in single quotes ?



 $Test = Hello
Write-output $($Test)


I would like the result to be 'Hello' including the quotes.







powershell format






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 17:36









user7490700

285




285











  • Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
    – Jacob
    Nov 10 at 17:37











  • Thanks for quick reply :)
    – user7490700
    Nov 10 at 17:40










  • if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
    – Lee_Dailey
    Nov 10 at 17:46










  • Had to use it like this $("'$Test'") , but you pointed me in the right direction
    – user7490700
    Nov 10 at 17:49










  • I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
    – user7490700
    Nov 10 at 17:52
















  • Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
    – Jacob
    Nov 10 at 17:37











  • Thanks for quick reply :)
    – user7490700
    Nov 10 at 17:40










  • if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
    – Lee_Dailey
    Nov 10 at 17:46










  • Had to use it like this $("'$Test'") , but you pointed me in the right direction
    – user7490700
    Nov 10 at 17:49










  • I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
    – user7490700
    Nov 10 at 17:52















Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
– Jacob
Nov 10 at 17:37





Write-Output "'$($Test)'" should yield the result you're after (note the double quotes to create a String and then the single quotes within that String). Also you need to enclose "Hello" in quotes $Test = "Hello" so that PowerShell interprets it as a String.
– Jacob
Nov 10 at 17:37













Thanks for quick reply :)
– user7490700
Nov 10 at 17:40




Thanks for quick reply :)
– user7490700
Nov 10 at 17:40












if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
– Lee_Dailey
Nov 10 at 17:46




if you ALWAYS want the quotes to be there, you can save them as part of the $Var value. for instance, this "'Hello'" will print with the internal set of quotes. not that it is <double quote><single quote>Hello<single quote><double quote> and you can reverse them if you prefer to store the doubles instead of the singles. [grin]
– Lee_Dailey
Nov 10 at 17:46












Had to use it like this $("'$Test'") , but you pointed me in the right direction
– user7490700
Nov 10 at 17:49




Had to use it like this $("'$Test'") , but you pointed me in the right direction
– user7490700
Nov 10 at 17:49












I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
– user7490700
Nov 10 at 17:52




I don't always want them to be single quotes, as the message is just for generating email and a log file, I use the variable for other useful stuff in my script, but thanks anyway
– user7490700
Nov 10 at 17:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










With an expandable string (string interpolation):



# To embed *expressions*, additionally enclose in $(...); e.g., "'$($Test+1)'"
"'$Test'"


As a general aside: In order to merely output a value, there's no need for Write-Output, because PowerShell implicitly outputs expression / command results (that are neither captured nor redirected).



You can pass the expression above as-is as an argument to a command, there is no need for $(...), the subexpression operator; sticking with the Write-Output sample command:



Write-Output "'$Test'"


Use expandable strings as a convenient way of embedding the default string representation of a variable value or expression result in a string.




With -f, the string-formatting operator (internally based on String.Format):



"'0'" -f $Test # 0 is a placeholder for the 1st RHS operand

# Enclose in (...) to pass the expression as an argument to a command:
Write-Output ("'0'" -f $Test)


The -f operator gives you more control over the resulting string representation, allowing you to perform operations such as padding and selecting the number of decimal places for floating-point numbers.



Note, however that this approach is suitable only for scalars, not arrays (collections).




With string concatenation (+):



"'" + $Test + "'"

# Enclose in (...) to pass the expression as an argument to a command:
Write-Output ("'" + $Test + "'")


This a more verbose alternative to string expansion that makes the operation being performed more obvious.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241654%2fexpand-variable-and-add-results-in-single-quotes%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    With an expandable string (string interpolation):



    # To embed *expressions*, additionally enclose in $(...); e.g., "'$($Test+1)'"
    "'$Test'"


    As a general aside: In order to merely output a value, there's no need for Write-Output, because PowerShell implicitly outputs expression / command results (that are neither captured nor redirected).



    You can pass the expression above as-is as an argument to a command, there is no need for $(...), the subexpression operator; sticking with the Write-Output sample command:



    Write-Output "'$Test'"


    Use expandable strings as a convenient way of embedding the default string representation of a variable value or expression result in a string.




    With -f, the string-formatting operator (internally based on String.Format):



    "'0'" -f $Test # 0 is a placeholder for the 1st RHS operand

    # Enclose in (...) to pass the expression as an argument to a command:
    Write-Output ("'0'" -f $Test)


    The -f operator gives you more control over the resulting string representation, allowing you to perform operations such as padding and selecting the number of decimal places for floating-point numbers.



    Note, however that this approach is suitable only for scalars, not arrays (collections).




    With string concatenation (+):



    "'" + $Test + "'"

    # Enclose in (...) to pass the expression as an argument to a command:
    Write-Output ("'" + $Test + "'")


    This a more verbose alternative to string expansion that makes the operation being performed more obvious.






    share|improve this answer


























      up vote
      1
      down vote



      accepted










      With an expandable string (string interpolation):



      # To embed *expressions*, additionally enclose in $(...); e.g., "'$($Test+1)'"
      "'$Test'"


      As a general aside: In order to merely output a value, there's no need for Write-Output, because PowerShell implicitly outputs expression / command results (that are neither captured nor redirected).



      You can pass the expression above as-is as an argument to a command, there is no need for $(...), the subexpression operator; sticking with the Write-Output sample command:



      Write-Output "'$Test'"


      Use expandable strings as a convenient way of embedding the default string representation of a variable value or expression result in a string.




      With -f, the string-formatting operator (internally based on String.Format):



      "'0'" -f $Test # 0 is a placeholder for the 1st RHS operand

      # Enclose in (...) to pass the expression as an argument to a command:
      Write-Output ("'0'" -f $Test)


      The -f operator gives you more control over the resulting string representation, allowing you to perform operations such as padding and selecting the number of decimal places for floating-point numbers.



      Note, however that this approach is suitable only for scalars, not arrays (collections).




      With string concatenation (+):



      "'" + $Test + "'"

      # Enclose in (...) to pass the expression as an argument to a command:
      Write-Output ("'" + $Test + "'")


      This a more verbose alternative to string expansion that makes the operation being performed more obvious.






      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        With an expandable string (string interpolation):



        # To embed *expressions*, additionally enclose in $(...); e.g., "'$($Test+1)'"
        "'$Test'"


        As a general aside: In order to merely output a value, there's no need for Write-Output, because PowerShell implicitly outputs expression / command results (that are neither captured nor redirected).



        You can pass the expression above as-is as an argument to a command, there is no need for $(...), the subexpression operator; sticking with the Write-Output sample command:



        Write-Output "'$Test'"


        Use expandable strings as a convenient way of embedding the default string representation of a variable value or expression result in a string.




        With -f, the string-formatting operator (internally based on String.Format):



        "'0'" -f $Test # 0 is a placeholder for the 1st RHS operand

        # Enclose in (...) to pass the expression as an argument to a command:
        Write-Output ("'0'" -f $Test)


        The -f operator gives you more control over the resulting string representation, allowing you to perform operations such as padding and selecting the number of decimal places for floating-point numbers.



        Note, however that this approach is suitable only for scalars, not arrays (collections).




        With string concatenation (+):



        "'" + $Test + "'"

        # Enclose in (...) to pass the expression as an argument to a command:
        Write-Output ("'" + $Test + "'")


        This a more verbose alternative to string expansion that makes the operation being performed more obvious.






        share|improve this answer














        With an expandable string (string interpolation):



        # To embed *expressions*, additionally enclose in $(...); e.g., "'$($Test+1)'"
        "'$Test'"


        As a general aside: In order to merely output a value, there's no need for Write-Output, because PowerShell implicitly outputs expression / command results (that are neither captured nor redirected).



        You can pass the expression above as-is as an argument to a command, there is no need for $(...), the subexpression operator; sticking with the Write-Output sample command:



        Write-Output "'$Test'"


        Use expandable strings as a convenient way of embedding the default string representation of a variable value or expression result in a string.




        With -f, the string-formatting operator (internally based on String.Format):



        "'0'" -f $Test # 0 is a placeholder for the 1st RHS operand

        # Enclose in (...) to pass the expression as an argument to a command:
        Write-Output ("'0'" -f $Test)


        The -f operator gives you more control over the resulting string representation, allowing you to perform operations such as padding and selecting the number of decimal places for floating-point numbers.



        Note, however that this approach is suitable only for scalars, not arrays (collections).




        With string concatenation (+):



        "'" + $Test + "'"

        # Enclose in (...) to pass the expression as an argument to a command:
        Write-Output ("'" + $Test + "'")


        This a more verbose alternative to string expansion that makes the operation being performed more obvious.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 13:04

























        answered Nov 10 at 17:55









        mklement0

        121k20233262




        121k20233262



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241654%2fexpand-variable-and-add-results-in-single-quotes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto