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.Length
$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)
$text.SubString(3, 4)
$text.ToLower()
$text.ToUpper()
$text.Trim()
$text.TrimEnd()
$text.TrimStart()

Putting Strings Together

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.

Taking Strings Apart

There are times when you only want part of a string, here is a handy way to get the last part of a string, from a given part of the string:

$strExample = "Hello World"
$strExample.IndexOf("World") # output - 6
$strExample.SubString($strExample.IndexOf("World")) # output - World

Encoded Strings

Sometimes passwords are encoded in Base 64 and sometimes it is other pieces of text, as, for example Base 64 XML is quite easy to insert into another XML document, without getting "corrupted". The way you decode is as follows:

$strDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($strEncoded))
You might need to use "UNICODE" or "ASCII" instead of "UTF8" but you can see the idea.

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

More...

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*', couple 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.