Expand variable and add results in single quotes
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
powershell format
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 11 at 13:04
answered Nov 10 at 17:55
mklement0
121k20233262
121k20233262
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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