2

In PowerShell, few programs starts with a @' and ends with '@, but when I type @' and press enter in the PowerShell prompt it throws an error. Can anyone explain how can I go about this?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
dek
  • 21
  • 1
  • 1
  • 2

2 Answers2

7

@'...'@ and @"..."@ are "Here Strings" (documented in about_quoting_rules):

HERE-STRINGS

The quotation rules for here-strings are slightly different.

A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally. A here-string can span multiple lines. All the lines in a here-string are interpreted as strings even though they are not enclosed in quotation marks.

Like regular strings, variables are replaced by their values in double-quoted here-strings. In single-quoted here-strings, variables are not replaced by their values.

You can use here-strings for any text, but they are particularly useful for the following kinds of text:

-- Text that contains literal quotation marks
-- Multiple lines of text, such as the text in an HTML or XML document
-- The Help text for a script or function

A here-string can have either of the following formats, where represents the linefeed or newline hidden character that is added when you press the ENTER key.

Double-quotes:

   @"<Enter>
    <string> [string] ...<Enter>
    "@

Single-quotes:

   @'<Enter>
    <string> [string] ...<Enter>
    '@

In either format, the closing quotation mark must be the first character in the line.

A here-string contains all the text between the two hidden characters. In the here-string, all quotation marks are interpreted literally. For example:

   @"
    For help, type "get-help"
    "@

The output of this command is:

   For help, type "get-help"

Using a here-string can simplify using a string in a command. For example:

   @"
    Use a quotation mark (') to begin a string.
    "@

The output of this command is:

   Use a quotation mark (') to begin a string.

In single-quoted here-strings, variables are interpreted literally and reproduced exactly. For example:

   @'
    The $profile variable contains the path
    of your Windows PowerShell profile.
    '@

The output of this command is:

   The $profile variable contains the path
    of your Windows PowerShell profile.

In double-quoted here-strings, variables are replaced by their values. For example:

   @" 
    Even if you have not created a profile,
    the path of the profile file is:
    $profile.
    "@

The output of this command is:

   Even if you have not created a profile,
    the path of the profile file is:
    C:\Users\User01\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

Here-strings are typically used to assign multiple lines to a variable. For example, the following here-string assigns a page of XML to the $page variable.

   $page = [XML] @"
    <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10"
    xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" 
    xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
    <command:details>
            <command:name>
                   Format-Table
            </command:name>
            <maml:description>
                <maml:para>Formats the output as a table.</maml:para>
            </maml:description>
            <command:verb>format</command:verb>
            <command:noun>table</command:noun>
            <dev:version></dev:version>
    </command:details>
    ...
    </command:command>
    "@

Here-strings are also a convenient format for input to the ConvertFrom-StringData cmdlet, which converts here-strings to hash tables. For more information, see ConvertFrom-StringData.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
2

@' and '@ mark the beginning and end of a Here-String. Typing @' and then pressing Enter in a PowerShell console should normally give you the line continuation prompt (>>):

PS C:> @'
>> _

If you get an error you most likely didn't type a single (or double) quote, but a forward or backtick or some kind of typographic quote. If that's the case you should be getting an "unrecognized token" error like this:

PS C:\> 
At line:1 char:1
+ @´
+ ~
Unrecognized token in source text.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnrecognizedToken
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328