In PowerCLI, even a string is an object. You can list the members of a string object using the Get-Member cmdlet that you have seen before. Let's go back to our example in Chapter 2, Learning Basic PowerCLI Concepts. First, we create a string "Learning PowerCLI" and put it in a variable called $String. Then, we take this $String variable and execute the Get-Member cmdlet using the $String variable as the input:
You see that a string has a lot of methods, one property, and a special type of property called ParameterizedProperty. Let's first use the Length property. To use a property, type the object name or the name of the variable containing the object; then type a dot; finally, type the property name. For the string, you could use both of the following command lines:
You see that the Length property contains the number of characters of the string "Learning PowerCLI", 17 in this case.
Property names in PowerShell are not case-sensitive, so you could type the following command and still get the same results:
PowerCLI C:\> $String.length17
ParameterizedProperty is a property that accepts a parameter value. The ParameterizedProperty property's Chars can be used to return the character at a specific position in the string. You have to specify the position, also called the index, as a parameter to Chars. Indexes in PowerShell start with 0. Therefore, to get the first character of the string, type the following command:
PowerCLI C:\> $String.Chars(0)L
To get the second character of the string, type the following command:
PowerCLI C:\> $String.Chars(1)e
You cannot use -1 to get the last character of the string as you can do with indexing in a PowerShell array. You have to calculate the last index yourself; this is calculated by subtracting 1 from the length of the string. Therefore, to get the last character of the string, you can type the following command:
PowerCLI C:\> $String.Chars($String.Length - 1)I
PowerShell has more types of properties, such as AliasProperty, CodeProperty, NoteProperty, and ScriptProperty.
AliasProperty is an alias name for an existing property
CodeProperty is a property that maps to a static method on a .NET class
NoteProperty is a property that contains data
ScriptProperty is a property whose value is returned from executing a PowerShell scriptblock
Using methods
Using methods is as easy as using properties. You type the object name, or the name of a variable containing the object; then you type a dot; and, after the dot, you type the name of the method. For methods, you always have to use parentheses after the method name. For example, to modify a string to all uppercase letters, type in the following command:
PowerCLI C:\> $String.ToUpper()LEARNING POWERCLI
Some methods require parameters. For example, to find the index of the P character in the string, you can use the following command:
PowerCLI C:\> $String.IndexOf('P')9
The character P is the tenth character in the "Learning PowerCLI" string. But because indexes in PowerShell start with 0 and not 1, the index of the P character in the string is 9 and not 10.
One very useful method is Replace, which you can use to replace a character or a substring with another character or string or with nothing. For example you can replace all instances of the e characters in the string with the u character, as follows:
There is also a –Replace operator in PowerShell. You can use the -Replace operator to carry out a regular expression-based text substitution on a string or a collection of strings as shown in the following command line:
Although both have the same name, the string Replace method and the –Replace operator are two different things. There is no –ToUpper operator; as you can see in the following example, this gives an error message:
You can use more than one method in the same command. Say you want to replace Learning with Gaining and that you want to remove the characters C, L, and I from the end of the string using the TrimEnd method. For this, you can use the following command:
PowerCLI C:\> $String.Replace('Learning','Gaining').TrimEnd('CLI')Gaining Power