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 NowNext up:
- Formula vs. measure in PowerPivot
- PhotoShop in action - Part 1
- Stop whinging; it’s boring!
- Windows Store for Business
- Create with Adobe’s Capture Mobile app: Creating Brushes
- Quick Tuts: Excel 2010 - vLookups simplified
- Social tagging in SharePoint 2010
- Tailoring your document with Styles
- 4 guides to help you become a PowerPoint pro
- Easy customisations in Word 2013
Previously
- How-to: Give access to a list only in SharePoint 2013
- Managing for excellence: An innovative approach to managing performance
- How-To series: Password protect sections in OneNote 2013
- SharePoint 2016 social features
- 5 posts to help you become a better communicator
- How to omit dates in an Excel chart axis
- PowerShell Basics Series – Measuring objects (Part 2)
- Create cascaded drop-downs in Microsoft Access forms
- Working with Styles - Part 2
- What is Continual Service Improvement (CSI)?