PowerShell Basics Series – Manipulating strings

 Jul 22, 2016

We all should know by now that PowerShell is object oriented (i.e. it primarily retrieves and manipulates .NET Framework objects), but that does not mean that PowerShell is any kind of slouch when it comes to playing with strings – it has quite a few tricks up its sleeve when it comes to string manipulations. In fact, strings are just another type of object in PowerShell, called a System.String type. That means that it has one Property (called length), a ParameterizedProperty called Chars, and a whole swag of Methods (actions) we can apply to our string values. We can see this simply by running a PowerShell “x” | Get-Member cmdlet (anything enclosed in quotation marks is taken as a string by default in PowerShell) e.g.:

By the way, a ParameterizedProperty is simply a property that requires additional parameters to access the value of that property. For example if we are setting a value in an array, we need to know not just the value we are setting, but also the location (the index) within that array. In the example above, if we wanted to use the ParameterizedProperty of Chars, we first would need to indicate the character position within the string before we can retrieve that specific character.

Now before I demo some examples, there are a couple of important things I want you to take note of here. Firstly, whenever we action a Method, the action applies to the OUTPUT and not to the actual string contents i.e. the value originally assigned to the string does not change, no matter what Method we apply. If you want to actually change the string’s value you can easily assign the method’s output to another variable i.e:

And Secondly, the Method name is always followed by parentheses, even when the method requires no arguments, and there is no space between the Method name and the parentheses. Properties on the other hand (such as Length), do not use parentheses.

So, let's look at some basic examples:

OK so the first 2 examples show the difference between accessing a property (the .Length) and a Method (the .Chars(8)). Also note what looks like an inconsistency – the Length property counts up from 1, whilst the Chars(8) counts up from 0. This is because the Chars(8) Method is treating the string as an array, and in PowerShell the first array position is always 0.

In addition the first 2 examples are working directly with a string (indicated by the quotation marks), but the following examples show assigning the string to a variable, and then working on the output of that variable. The .ToUpper() and .ToLower change the output to uppercase or lowercase respectively, and the .Replace(“o”,”_”) replaces the nominated character(s) – (e.g.the “o”’s) within the string with alternative character(s) (e.g. the “_”). The .substring(8,5) Method extracts the substring beginning at the 8th character (remember starting from 0) and extracting the next 5 characters.

Of course, in addition to a string type’s Methods, PowerShell itself allows a number of different operators to manipulate strings – operators such as –Join, -Replace and –Split. For example:

Here examples 1 and 2 show two sides of the same coin – example 1 is using the –Join to concatenate multiple strings, and example 2 is using the –Split to break up one string into multiples. Example 3 is showing the –Replace operator to replace a specified character with alternative character(s). Other Operators you can check out for yourselves are –In, -Contains, -Match and –As.

Note that these operators provide functionality that quite often overlaps the functionality of the string type’s methods, and you are welcome to use either Methods or Operators to accomplish your desired task, but the thing to note here is that Methods are case-sensitive by default, and Operators are case-insensitive by default. Nor are these the only possibilities in PowerShell in manipulating strings – try for yourself the following, where we are simply using the plus sign to join multiple strings together:

" One"+" Two"+" Three"+" Four"

You can’t say PowerShell doesn’t give you plenty of options!

Have a play with these examples yourself, and I will be back in my next blog with some date and time value manipulations for you.

How do your Excel skills stack up?   

Test Now  

How do
your Excel skills
stack up?

Grade your skills now

About the Author:

Gordon Cowser  

With over 22 years real world and training experience, Gordon is our most senior IT Infrastructure trainer. His expertise includes but is not limited to; Microsoft Server and Client OS, Messaging, Collaboration, Active Directory and Network Infrastructure. Gordon also specialises in SharePoint technologies training in both technical and end user aspects. With his extensive skill-set he brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. A very approachable and experienced training professional, he has the ability to establish credibility fast with students at all levels.

Read full bio
Back to top