learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-string-substitutions?view=powershell-5.1
1 Users
0 Comments
13 Highlights
2 Notes
Tags
Top Highlights
A double quoted string allows the substitution but a single quoted string doesn't.
$message = "Date: $(Get-Date)"
$message = "Time: $($directory.CreationTime)"
It gets cluttered quickly and hard to debug. I either run the command and save to a variable or use a format string.
If you want to -join some strings without a separator, you need to specify an empty string ''. But if that is all you need, there's a faster option.
[string]::Concat('server1','server2','server3') [string]::Concat($servers)
Join-Path -Path 'c:\windows' -ChildPath $folder
This also goes well with Split-Path and Test-Path.
Remember that a string is just an array of characters. When you add multiple strings together, a new array is created each time.
StringBuilder is also very popular for building large strings from lots of smaller strings. The reason why is because it just collects all the strings you add to it and only concatenates all of them at the end when you retrieve the value.
Delineation with braces This is used for suffix concatenation within the string. Sometimes your variable doesn't have a clean word boundary.
Write-Host "$test $tester ${test}ter"
Write-Host "{0} {1} {0}ter" -f $test, $tester
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.