Search notes:

PowerShell: Literals

PowerShell has four types of literals:
Note that there is no char literal, so, unlike in for example C or C++, both, 'f' and "f" denote a string.

Integer literals

Data type

The data type of numerical (integer) literals between -2147483648 and 2147483647 is a 32-bit signed integer.
Outside of that range, but within -9223372036854775808 and 9223372036854775807, the data type is a 64-bit signed integer.
Outside of that range, but within -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335 the data type is System.Decimal.
Outside of that range, the data type becomes a System.Double.
An integer literal allows to specify its data type with one of the following suffixes. Most of those suffixes were added only with PowerShell version 6.2, though
Suffix Type
y, uy System.SByte, System.Byte
s, us System.Int16, System.UInt16
l, ul System.Int64, System.UInt64
u System.Int32
n System.Numerics.BigInteger

Multipliers

PowerShell provides a few multipliers:
kb Kilobyte = 2¹⁰
mb Megabyte
gb Gigabyte
tb Terabyte
pb Petabyte
PS C:\> 4kb * 4
16384
Compare these multipliers with the prefixes for data units (such as kilo-meter).

Hexadecimal notation

An integer can be represented with the hexadecimal notation by prefixing the hexadecimal value with 0x:
PS C:\> 0x10 * 0x0f
240
Literals written with the hexadecimal notation are interpreted as signed 32-bit integer:
PS C:\> 0x7fffffff
2147483647

PS C:\> 0x80000000
-2147483648

String literals

Embed characters in hexadecimal notation

In order to insert characters in hexadecimal notation, $([char]0x..) must be used, for example:
"hello $([char]0x40) world$([char]0x0d)$([char]0x0a)next line"

Here document

The @"…"@ can be used like a here document. Variables are expanded and the string may contain apostrophes:
$num   =  42
$value = 'forty-two'

$text = @"
The number is: $num,
and it's value "$value"
"@

write-host $text
The number is: 42,
and it's value "forty-two"
Note that there must be no character between the starting @" and the following end of line (Error message No characters are allowed after a here-string header but before the end of the line.).
Similarly, the terminating "@ must be placed at the start of a line.

Embeding single and double quotes

In order to embed the character that delimits the string literal into the literal itself, the character must be doubled:
'Five o''clock'
"They said ""hello""."

Backslash, backtick

The back-slash character does not have a special meaning (such as escaping the next character) in string literals:
write-host "P:\ath\to\dir"
write-host 'P:\ath\to\dir'
In double-quote string literals, the escape character is the backtick:
"They said `"hello`"."

See also

Literals in Python

Index