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.LastIndexOf(".") - handy for finding the last dot in a filename
$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) - get the string without the first 3 characters
$text.SubString(3, 4) - get up to 4 characters, starting with the 3rd one
$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.

If you have an array of strings, for example:
$StringArray = @("aa", "bb", "cc", "dd")
Then you ca easily join them together with the -Join operator as follows:
$StringArray -Join(":")
Which returns "aa:bb:cc:dd", using the specified separator. You can learn more about this handy operator at about_Join - PowerShell | Microsoft Docs.

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

Another way to take strings apart is to use the split() method.

There is also the -Split operator, which can be equally useful, here are a couple of examples:

$a, $b = "&:@:#" -Split ":"

$a, $b, $c = "&:@:#" -Split ":"

The first one sets $a to "&" and $b to "@","#" where $b is now a collection. The second one does as you might expect, setting the three variables to the three characters.

Encoded Strings

Sometimes passwords are encoded in Base64 and sometimes it is other pieces of text, as, for example Base64 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.

I have written an example script at powershell/general/Base64EncodeDecode.ps1 · master · Geoff Lawrence / Geoff Does Stuff · GitLab. There is all this and more at Windows PowerShell Tip: The String’s the Thing and PowerShell: String Formatting - TechNet Wiki

Searching

A simple example is this:
'Hello World from Geoff Does Stuff' -like '*geoff*'
There are a 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.

There is more help on doing grep style searching with PowerShell at The Grep of PowerShell [Tutorial].

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 { "" }

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.

Useful Articles

Parsing Text with PowerShell (1/3) | PowerShell Team Blog
Parsing Text with PowerShell (2/3) | PowerShell Team Blog
Parsing Text with PowerShell (3/3) | PowerShell Team Blog