PowerShell String Theory

PowerShell String Theory

  • Comments 7
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with strings.

Microsoft Scripting Guy, Ed Wilson, is here. This has been an exciting week around the Scripting Household. From our lanai, we can see several little rabbits hopping around—they seem to like the clover. We have seen some butterflies (which in past years, we have not seen), and the hummingbirds are back. It is a great time to be out and about. Sitting in a swing, observing nature is a fundamental that helps me keep a positive mental outlook.

Another fundamental is working with strings in Windows PowerShell. In the past (like back in the VBScript days), I hated working with strings. I could do it, but it did not seem like much fun to me. I would rather pull weeds out of the garden than spend a day manipulating VBScript strings. Luckily, Windows PowerShell makes that a bit easier to handle.

A string is an object

By now, you have probably heard that everything in Windows PowerShell is an object. This is one reason why the Get-Member Windows PowerShell cmdlet is so very important. It provides insight into the objects with which one might be confronted. Here is an example of piping a string object to Get-Member and looking at the methods that are available:

Image of command output

String objects

String objects have a single property. Properties describe things. Therefore, I can use the available string property to describe the string to me. What if I want to describe what am I most interested in? In this example, I am interested in how long is the string. So the property I have available to me is Length as shown here:

PS C:\> $a = "string"

PS C:\> $a | Get-Member -MemberType property -Force

   TypeName: System.String

 

Name   MemberType Definition

----   ---------- ----------

Length Property   int Length {get;}

 

PS C:\> $a.Length

6

Strings have lots of methods

Windows PowerShell strings have lots of methods. Again, I can use the Windows PowerShell Get-Member cmdlet to list the string methods. These are shown here:

Image of command output

The string object is well documented on MSDN (for more information, see String Class), and MSDN is the primary source of documentation for all the methods and other members of the objects. Most of the methods are really easy to use. For example, to find the position of a particular letter within a string, I use the IndexOf method. Here is an example of that technique:

PS C:\> $a = "strings"

PS C:\> $a.IndexOf("s")

0

PS C:\>

            PS C:\> $a = "strings"

PS C:\> $a.IndexOf("s")

0

    Note  The IndexOf method is zero based. This means that it begins counting at zero. The Length property is 1 based,
    so it begins counting with 1.

The IndexOf method has a couple of overloads. This means that the same method can be used in different ways. As I showed previously, the basic way to use IndexOf is to tell it to look for a letter. When it does this, it begins looking at the first position (index 0). When it finds a match, it is done.

The second way to use IndexOf is to tell it what to look for and where to begin looking. In this example, the word strings is seven characters long. The first position that contains the letter “s” is in position zero. Therefore, if I tell it to begin looking for the letter “s” beginning with the second letter (index position 1), it will find the letter “s” in index 6 (the seventh position in our word). This is shown here:

PS C:\> $a = "strings"

PS C:\> $a.IndexOf("s",1)

6

PS C:\>

There is another way to use the IndexOf method that combines the first two ways.

 Image of text bubble

 Image of Dr. Scripto

The last way I want to look at using the IndexOf method today permits me to specify the letter I want to look for, the place I want to begin looking, and the number of characters that I want to search. After creating my string, I use the IndexOf method to look for the letter "s" beginning in position 1. (Remember this is the second letter in the string; and therefore, I miss the initial letter "s"). I tell it to seek from position 1 to position 5 (the second letter through the sixth letter, which in this case is the letter "g"). It returns -1. When the IndexOf method returns the number -1, it means that it did not find a match.

PS C:\> $a = "strings"

PS C:\> $a.IndexOf("s",1,5)

-1

The next time, I use the IndexOf method, I again specify the letter "s," and I again begin searching at the second letter (index 1). But this time, I search through to the seventh letter (index 6). This time, the IndexOf method returns 6, which means it found a match in the sixth index position. This is shown here:

PS C:\> $a.IndexOf("s",1,6)

6

PS C:\>

    Note  According to MSDN, it appears that there are at least 9 ways to use the IndexOf method.

There is your simple introduction to using Windows PowerShell to work with strings. String Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • "The Length property is 1 based, so it begins counting with 1." ???

    C:\> $s = ""
    C:\> $s.Length
    0

  • Ed LOVE the Title!
    I get a BIG Bang out of it ! :)

    Sean

  • @Graham this is correct. Many things (like arrays) use a zero based index, in which the first element is actually zero, and not one. So it can be a bit confusing. I am not saying this is wrong, but I am just pointing out a potential for confusion. Here are some more examples:
    PS C:\> $s = 'a'
    PS C:\> $s.Length
    1
    PS C:\> $array = 1,2
    PS C:\> $array[0]
    1
    PS C:\>

  • @Sean thank you. I am glad you enjoyed it. I am beginning to say, each day, what would Sean do ...

  • thank you

  • thanks