PowerShell Here-Strings

Here-strings for multi-line text, template generation, and embedded quotes.

Here-Strings Quick Reference

Expandable here-string (double-quoted — variables expand)
$hostname = "ise-01"
$port = 8443
$json = @"
{
  "host": "$hostname",
  "port": $port
}
"@
# Output: {"host": "ise-01", "port": 8443}
Literal here-string (single-quoted — no expansion)
$query = @'
SELECT endpoint_mac, identity
FROM radius_authentications
WHERE timestamp > DATEADD(hour, -1, GETDATE())
'@
# $variables inside are literal text, not expanded
JSON payload for REST API
$body = @"
{
  "name": "$name",
  "description": "$($desc.Replace('"','\"'))"
}
"@
Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json"
LDAP filter string
$filter = @'
(&(objectClass=user)(memberOf=CN=ISE-Admins,OU=Groups,DC=inside,DC=domusdigitalis,DC=dev))
'@
Get-ADUser -LDAPFilter $filter
Script block from here-string
$script = @'
Get-Service | Where-Object Status -eq Running | Select-Object Name, DisplayName
'@
Invoke-Command -ComputerName $servers -ScriptBlock ([scriptblock]::Create($script))
Write config file
Set-Content -Path config.conf -Value @'
# Generated config
server.port=8080
server.host=0.0.0.0
'@

See Also

  • Basics — string fundamentals