Basics
String literals are one of essential constructs of PS. There are lot of ways to create and manage them.There is a major difference between single and double quoted string. Single quoted literals are not processed by PowerShell at all, so you can not add variables, escaped characters and other cool stuff to them.
- $var = 'Append'
- # Substitution:
- Write-Host 'No substitution: $var' # No substitution: $var
- Write-Host "Substitution: $var" # Substitution: Append
- # Double quote char to add same quotes as literal quoted:
- Write-Host 'Don''t forget that " is originally inch character' # Single quote doubled
- Write-Host "Don't forget that "" is originally inch character" # Double quote doubled
- # To use escape chars escape them with ` character.
- # Find it on [~] key, it is like small back slash.
- # It works only with double quoted strings
- Write-Host "First line`r`nSecong line" # line break
- Write-Host "First`tSecond" # tabulation
$
', '`
' and '"
' characters in it.Expression substitution
First advanced feature I want to mention is a complex expression substitutions. You are not limited with simple variable substitution in double quoted strings. You can add expressions of any complexity to your string, and they will be executed. Just wrap expression inside double quoted string with
$( ... )
:- $arr = @(4, 8, 15, 16, 23, 42)
- $hashtable = @{
- Name = "James";
- Surname = 'Bond';
- }
- Function Add-Numbers ($a,$b) {
- $a + $b
- }
- Write-Host "Answer to the Ultimate Question of Life is $($arr[5])"
- Write-Host "My name is $($hashtable.Surname), $($hashtable.Name) $($hashtable.Surname)."
- Write-Host "13 + 43 = $(Add-Numbers 13 41)"
Here-strings
When you want to add some big literal with line breaks, both single and double quotes in it (great example is XML) you do not need to shield all them and change line breaks to "`r`n" stuff. There is such thing as 'here-strings' in PowerShell. They should start with @" and line break and should end with line break "@.- # Notice that line break after @" and before "@ is not included to string
- $hereString = @"
- First line
- Quotes of both types: " and '
- Grave accent: `
- Last line
- "@
Format string
You can use ordinary format string via special keyword -f. As in .NET you can use format item to add some additional parameters to format:
- Write-Host ( 'My name is {1}, {0} {1}' -f 'James', 'Bond' )
- # My name is Bond, James Bond
- Write-Host ( 'Double formatting {0:F3}' -f 1.2 )
- # Double formatting 1,200
- Write-Host ( 'DateTime format {0:MM/dd/yy H:mm:ss zzz}' -f [DateTime]::Now)
- # DateTime format 03.03.13 21:32:34 +02:00
- # Error!
- Write-Host 'Simple {0}' -f 'format'
Performance
In rare cases single quoted string can give you performance boost if you prefer concatenation of single quoted strings to substitution inside of double quoted string. Try this example, and you will find, that concatenation is almost two times faster:- $s = New-Object System.Diagnostics.Stopwatch
- $s.Start()
- for ($i = 0; $i -lt 1000000; $i++) {
- $var = 'world'
- $a = "Hello $var"
- }
- $s.Stop()
- Write-Host 'Double quotes with substitution: ' + $s.Elapsed
- $s.Reset()
- $s.Start()
- for ($i = 0; $i -lt 1000000; $i++) {
- $var = 'world'
- $a = 'Hello ' + $var
- }
- $s.Stop()
- Write-Host 'Single quotes and concatenation: ' + $s.Elapsed