String
String Interpolation
Interpolated string should quote with "
.
Use $
to interpolate:
- variable
- expression
- member accessing
"Profile Path: $Profile" # variable
"1 + 1 = $(1 + 1)" # expression
# functionaly identical
"cwd: $($pwd.Path)" # Member accessing
"cwd: $pwd"
"$(@(1, 2, 3)[0])"
2
3
4
5
6
7
8
NOTE
$()
is being called SubExpression Operator in PowerShell.
Verbatim String
Verbatim string should quote with '
. Can contains new lines and
'
It''s a verbatim string!
and this is a new line!
'
2
3
4
NOTE
Verbatim string does not allow interpolation in PowerShell which differs from C#
.
Raw String
NOTE
Raw string is being called Here String in PowerShell.
Here String typically allows string contents starting at the second line and truncates the last newline.
Use @'...'@
or @"..."@
to present Verbatim Here String or Interpolated Here String.
@"
<foo>$Profile</foo>
"@
@'
{
"foo": {
"bar": []
}
}
'@
2
3
4
5
6
7
8
9
10
11
NOTE
You can have quotation mark "
in @"..."@
and for '
respectively.
Character Escaping
Quotation Mark
Use double "
to escape "
in a interpolated string.
"""""" # ""
"I quote, ""haha!""" # I quote, "haha!"
2
Or use `
to escape the same quotation mark in the containing quotation.
"I quote, `"haha!`""
Use double '
to escape '
in a verbatim string.
'''''' # ''
'I quote, ''haha!''' # I quote, 'haha!'
2
NOTE
Special Characters
Other special characters should only be escaped by `
, and only available in a double quote string or double quote here string.
"Line with a new line `n"
@"
`tstarts with tab
"@
'no escape happened here! `n'
2
3
4
5
6
7
Arithmetic with Numeric
PowerShell will try to convert the string on the right operand to the same type as left operand. Exception will be raised if conversion failed.
1 + '2' # 3
'2' + 1 # '21'
[datetime]::Now + '00:00:15:00' # adds 15 minutes
2
3
Comparison
All comparison operators have its case-sensitive version with leading -c
-gt
->-cgt
-lt
->-clt
- ...
'Apple' -eq 'apple' # True
'Apple' -ceq 'apple' # False
2
Split & Join
'1,2,3' -split ',' # 1 2 3 as strings
(gci -file) -join ',' # ToString is invoked to evaluated objects to string.
2
Join from Objects
Join-String
is a more generic solution for joining multiple items as a single string. -join
operator handles only collection while Join-String
can accept pipeline input by value.
-Property(0)
: pick one property name or script block to be joined in the resultps1gci -file | Join-String FullName gci -file | Join-String { $_.FullName }
1
2-Separator(1)
: separator between each two propertiesps1gci -file | Join-String FullName ', '
1-DoubleQuote
: optionally surround each item by double quote-SingleQuote
: optionally surround each item by single quote-OutputPrefix
: specify a initial leading string for the result-OutputSuffix
: specify a trailing string for the result-FormatString
: use string format to reshape the string from-Property
(you can do the similar in-Property
too)ps1gci -file | Join-String Name "$([Environment]::NewLine)" -FormatString 'File name: {0}'
1
Match & Replace
PowerShell has two kinds of matching strategy for strings.
-match
for regex matching.-like
for wildcard matching.
While -replace
only supports regex.
TIP
Match and replace is case-insensitive by default. Use -cmatch
, -cnotmatch
, -creplace
, -csplit
for case-sensitive scenarios.
'Janet is a girl' -match 'Jane' # True
'Janet is a girl' -replace '^Janet', 'Jane'
'Janet is a girl' -replace '^Janet', 'Jane' -replace 'is', 'is not' # replace multiple times inline.
2
3
All previous matches and captured groups can be accessed in $matches
builtin variable. $matches
is a HashTable, you can access named capture group by group name and use index for unamed group.
if ('John Smith' -match '^(?<FirstName>\b\w+\b) (\b\w+\b)$') {
$matches.FirstName # John
$matches[0] # John Smith
$matches[1] # Smith
}
2
3
4
5
Enumerate in Replace PowerShell 6+
-replace
allows access to the macthed object by $_
typed as System.Text.RegularExpressions.Match
in a scriptblock to perform a more flexible action when replacing.
# Transform captured group in replace
'John Smith' -replace 'John', { $_.Value.ToLower() }
2
Format String
Template string syntax is the same as C#
. Standard numeric format like :C
, :X
are supported.
'This is a {0} string' -f 'format'
Repetition
Use *
to repeat a string.
'abc' * 2 # abcabc
Grep
Select-String
is a builtin cmdlet that does the similar thing like grep
in gnu coreutils. It can select one or more lines that matches the specified regex.
Select-String
returns one or more Microsoft.PowerShell.Commands.MatchInfo
that majorly includes:
Matches as
System.Text.RegularExpressions.Match[]
Line: content of the matched line
LineNumber: line number of the matched line
-SimpleMatch
: prevent interpretation of-Pattern
to a regex.-NotMatch
: returns lines that doesn't match the-Pattern
.-Path
: try match the name of a dir or content of a file-Raw
: return matched string instead ofMatchInfo
-Quiet
: return flag that indicates whether the match succeeded($true
) or not($null
)-AllMatches
: match all occurrences for each line.-List
: match first occurrence in file.-NoEmphasis
: cancel highlighting for matchesGrep single line from pipeline
# -Pattern is positional
help sls | sls -Pattern 'Position\??\s*\d'
2
- Grep multiple lines from above and below
# include 3 lines above and 5 lines below the macthed line
help sls | sls 'Position\??\s*\d+' -Context 3,5
2
- Grep from pipeline passed by property(
-Path
)
PS ~> gci /nix/store/ -dir | sls 'roslyn'
/nix/store/m4npcw66155bbi8i7ipp0bbp54wdwwlg-roslyn-ls-4.13.0-3.24577.4
/nix/store/np6bigb7d3lvpinw0mrdvj38xi67q6sa-roslyn-ls-4.12.0-2.24422.6
2
3
4
- Match from a file,
sls
has different format for content from-Path
which includes line number.
sls -Path ./foo.txt -Pattern 'foo'
gci *foo.txt | sls 'foo'
2
- Filter files match certain pattern quickly(doc said it is the most efficient way)
gci -file | sls -Pattern 'foo' -List | foreach Path
Convert to String
There's two way of general string conversion from object.
- formatting by
Out-String
ToString
ToString
is sometimes limited since there might not be always a override available, so it just returns the type fullname. While formatting is more generalized as we usually see in console output, especially when pipe it to a cmdlet to do text manipulation. That's the reason why we have Out-String
, which converts object to its formatted representation.
gci | Out-String | sls 'foo'
Out-String
has a dedicated parameter -Stream
which allow it to pipe the string single line each time. And PowerShell has a builtin function oss
as an alias for Out-String -Stream
gci | Out-String | sls 'foo' # match the entire string
gci | oss | sls 'foo' # match the matched line only
2
String Evaluation
Collection
Collections like Array and HashTable are formatted by separator stored in $OFS
(Output Field Separator)
"$(1, 2, 3)" # 1 2 3
$OFS = ', '
"$(1, 2, 3)" # 1, 2, 3
2
3
NOTE
$OFS
is not a builtin variable, you'll have to create it manually or pwsh uses space as the default separator.