Export (0) Print
Expand All
Expand Minimize

Converting the Windows Script Host Skip Method


Windows PowerShell


Definition: Skips a specified number of characters when reading from an input text stream.

Skip


Here we have yet another oddball method for working with standard streams like StdIn, StdOut, and StdErr. The Skip method enables you to skip over a specified number of characters when reading something typed in by a user. For example, consider this simple WSH script:


Wscript.StdOut.Write "Please enter your name: "
WScript.StdIn.Skip 3
strInput = WScript.StdIn.ReadLine
WScript.StdOut.Write strInput


After asking the user to enter his or her name we call the Skip method, asking the script to skip over the first 3 characters the user types in. We next call the ReadLine method in order to read the data entered by the user, then use the StdOut.Write method to echo this data – minus the first three characters – to the screen.

What does all that mean? Well, suppose the user types in the following:


Jonathan Haas


Here’s what gets echoed back to the screen:


athan Haas


As you can see, the first three letters (Jon) have been skipped, just like we wanted. (Of course, why we wanted this is a whole ‘nother story.)

Of course, if we hadn’t brought this up the odds are you’d never even consider writing a script that skipped over the first three characters typed in by a user. However, now that you know that Windows Script Host possesses this capability you’re undoubtedly obsessed with adding this same functionality to your Windows PowerShell scripts. Well, you can relax; here’s some PowerShell code that mimics the Skip method:


$strInput = Read-Host "Please enter your name"
$intLength = $strInput.Length
$strInput.substring(3, $intlength - 3)


In our first line above we’re using the Read-Host cmdlet to prompt the user to enter his or her name, storing the entered data in a variable named $strInput. In the second line, we use the Length property to determine the total number of characters in the string. Why do we do that? Well, in just a second we’re going to tell PowerShell to create a substring from the characters in $strInput. For starters, we’ll tell it where to begin: on character 3, the fourth character in the string. (Remember, the first character in a string is always in position 0; that put the fourth character in position 3. To skip the first three characters we need to skip characters 0, 1, and 2, and start off with character 3.)

In addition, we need to tell PowerShell how many characters to grab. How many characters do we want it to grab? Well, we aren’t sure; that depends on how many characters the user types in. because that value can – and will – vary, we can’t specify a number here. However, we can easily calculate the number of characters to return: that’s going to be the total number of characters in the string, minus 3 (the minus 3 representing the 3 characters to be skipped).

All of that explains what line 3 in our script does:


$strInput.substring(3, $intlength - 3)


Here we’re simply taking a substring of the variable $strInput, skipping the first 3 characters and ending with the very last character in the string ($intLength -3 ). What will we get back if someone runs this script and enters the name Jonathan Haas? Why, we’ll get back this, of course:


athan Haas



See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page

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