Prof. Powershell

Stringing Along

Use a PowerShell object's methods to do many of the same tasks for which you'd write VBscript code in long form.

In VBscript and other scripting languages, you typically have to learn a large number of functions and commands to achieve some formatted result or to parse data. You might use the InStr function to find the position for a given character in a string, or use Mid to return a substring. You can achieve a lot of the same results in PowerShell, but you don't have to learn or memorize a ton of external functions.

Because everything in PowerShell is an object, we can use an object's methods to accomplish many of these tasks. Let's look at string objects as an example.

First, create a string object in PowerShell and then pipe it to Get-Member:

PS C:\> $sample="PowerShell Rocks!"
PS C:\> $sample | get-member

Notice all those methods? We can use them to modify or parse the string in $sample. All methods in PowerShell require you to use () like this:

PS C:\> $sample.toUpper()
POWERSHELL ROCKS!

We've just invoked the ToUpper() method which displayed $sample in upper case letters. It did not change the value of $sample:

PS C:\> $sample
PowerShell Rocks!

It merely called the method and wrote the results to the pipeline. Let's look at a few more methods.

Often you need to return a portion of a string. In VBscript you would use the Left, Right and Mid functions. In PowerShell all you need is the SubString() method:

PS C:\> $sample.substring(0,5)
Power

A string in PowerShell is treated as an array and all arrays in PowerShell start counting at 0. In this expression, I'm returning a substring starting at 0 and containing five characters. It's also just as easy to get the middle portion of a string:

PS C:\> $sample.substring(11)
Rocks!

In this example I'm starting at position 11 and returning all remaining characters, as I don't specify a number of characters. What about string substitution? It gets even easier:

PS C:\> $sample.Replace("o","X")
PXwerShell RXcks!

The Replace() method replaces all 'o' characters with 'X'.

Let's wrap up this lesson with a little bit longer example that demonstrates string methods in action:

Get-Content c:\test\servers.txt | foreach {
   Write-Host $_.ToUpper() -foregroundcolor Green
   ((Get-WmiObject win32_operatingsystem -computername
   $_.Trim() ).csdversion).Replace("Service Pack ","SP").PadLeft(5)
}

Here's what's happening: All of the computer names in servers.txt are piped to ForEach-Object. Each computer name is written to the console in upper case. The computer name is then passed as a value for the -computername parameter for Get-WMIObject. I call the Trim() method to remove any extra spaces around the name. The Get-WMIObject line is actually long and runs several methods together. First I'm getting the CSDVERSION property of the WMI object which is the service pack information. I'm replacing the string "Service Pack" with "SP" and then the resulting string is padded to the left by five spaces. You may not use this in a production environment, but it's a good demonstration how you might use these string methods.

Next time, we'll look at methods for working with DateTime objects.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events