Working with Strings

Firstly it is important to note that there is a difference between strings enclosed with double quotes " and single quotes '. The single quote versions are literal, so commands embedded in $() are not processed. In addition when using double quotes you can use a backtick (`) as a prefix for special processing. So `$ means display a dollar and `" means output a double quote don't treat it as the end of the string. In addition `n gives a new line, `t a tab and there are more.

The following example are self explanatory:

$text = "Hello World"
$text.CompareTo("Hello") - returns 0 if match or 1 or -1 depending on how it compares when sorting
$text.Contains("llo") - returns true or false
$text.Equals("Hello All") - alternative to $text -ceq "Hello All"
$text.Equals("Hello WORLD", "CurrentCultureIgnoreCase") - alternative to $text -eq "Hello All"
$text.IndexOf("W") - returns the position of the specified string
$text.Replace("World", "Geoff")
$text.Split(" ") - returns an array of strings, split on specified characters, all of which are used as splitting characters
$text.StartsWith("He") - returns true or false
$text.SubString(3, 4)

Sometimes you need to bolt strings together:
$text = "Hello World" + ", from Geoff" - will concatenate
$text += " Lawrence" - another way to concatenate
$text = -Join("Hello", " ", "World") - a third way
Now you have that done, after the last step $text contains "Hello World", sometimes you need to insert variables like this into another string. The most robust way to do this is as follows:
$output = "Padding before$($text)and padding after", sometimes you can leave the dollar and brackets off but this can confuse what the actual variable name is, so this is the safe and robust technique.
$output = "Padding before{0}and padding after" -f $text - is an alternative and actually powerful technique.

There is all this and more at Windows PowerShell Tip: The String’s the Thing and PowerShell: String Formatting - TechNet Wiki

If you execute the following you will see a list of all the methods on a string:
"" | Get-Member
This is how I found some of the "trim" methods. However you can also view String Class (System)

One technique that is less obvious is testing whether a string "is null or empty", however PowerShell makes this really easy, so try this:
if ($text) { 'Contains Text' } else { 'Null or Empty' }, you will need to set $text to either some text or $null in order to test this and see how it works.
if (-Not $text) { Write-Host "It was null or empty"}, this is a more common use case.
Building on this, sometimes you only want to call a string method when it has a value, so something like this is very handy:
$destination = if ($source) { $source.Trim() } else { "" }

'Hello World from Geoff Does Stuff' -like '*geoff*', could of things to note, the search is not case sensitive, * means any number of characters and ? represents a single character. There is an alternative, you can use 'Hello World from Geoff Does Stuff' -match 'geoff', this does a Regular Expression search, so lookup the help on RegEx or read Powershell: The many ways to use regex. Not forgetting Select-String which is another technique worth looking up.

When comparing strings you can also use the following operators:

  • -eq - test for equality, not case sensitive
  • -ne - check not equal
  • -ceq - test for equality, case sensitive
  • -cne - check not equal but case sensitive
To be fair, you probably need to play around with these at the PowerShell prompt to make sure you understand them correctly.