Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Collapse the table of content
Expand the table of content
Expand Minimize

Converting VBScript's Mid Function

Windows PowerShell

Definition: Returns a specified number of characters from a string.


Sometimes the good stuff - at least when it comes to Oreo cookies and string values - is found in the middle. With Oreos you can get to the stuff in the middle by unscrewing the cookie; with string values you can get to the stuff in the middle by calling the Substring() method. In the following example we assign a string value to the variable $a and then use the SubString() method to retrieve a portion of that string. And what portion are we retrieving? To answer that, let’s first look at the two commands:

$a = $a.substring(2,3)

Notice that we’re passing Substring() two parameters: a 2 and a 3. The 2 represents the character position in the main string where we want to start grabbing letters. We want to start with the third character, so we pass a 2. No, that’s not a typo: we pass a 2 because the first character in the string is in position 0. That means the second character is found in position 1, and the third character - the one we’re interested in - resides in position 2.

The 3, meanwhile, indicates the number of characters we want to extract. In other words, we want to start at position 2 and grab the next three characters.

Pretty simple when you think about it.

When you run this command and then echo back the value of $a you should get the following:


Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft