Here String
This is a short blog on Here String. I decided to write down about this since I wanted to share the experiences with PowerShell features, that I heavily use.
I commonly use the following type of code to store large strings in a variable.
$data=@"
{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}
"@
{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}
"@
Why do I use the above instead of using a normal string?
$data="{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}"
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}"
Reasons:
1.The above code throws error due to the presence of string with double quotes
2. Using back ` is terrible for strings with many double quotes .
What was I doing to allow double quotes and have large strings declared in a variable?
I used @ at the end and beginning of the string and the string was able to store large data.
I did not know that the term used for this feature was Here string until I read various msdn blogs on PowerShell and came to know the Here String term.
What is Here String?
A simple String that allows you to declare multi ling string along with Quotes and variable values.
Make sure that you have a line break after the first @", else the following command throws error:
$HereString=@"My
"life"
is
in
my hands"@
"life"
is
in
my hands"@
The following code works correctly
$when="Always"
$HereString=@"
My
"life"
is
in
my hands $when
"@
$HereString=@"
My
"life"
is
in
my hands $when
"@
Below the output:
My
"life"
is
in
my hands Always
"life"
is
in
my hands Always
No comments:
Post a Comment