Microsoft PowerShell is a framework for running scripts and automating tasks using the Microsoft Windows operating system. PowerShell v2.0 is available for Windows 7 and can also be used with Windows XP if Service Pack 3 has been installed. Using the PowerShell "Split" function, a string can be broken up into parts using a custom delimiter. The result is returned in the form of an array, with each array element containing a part of the original string.
Start Windows PowerShell by clicking the Windows "Start" button and typing "powershell" into the "Search programs and files" text box. This will open the Windows PowerShell console.
Type "notepad split.ps1" in the PowerShell console to open Microsoft Notepad and create a new script called "split.ps1." Click "Yes" when prompted by Notepad to allow the new file to be created.
Video of the Day
Type the following commands into the new file, then save the file:
$text = "Windows PowerShell - Hello world!" $split = $text.split("-") echo $split echo $split
First, a string is created named "$text" which holds the text to be split. The string is then split using the PowerShell "Split" function, passing in the character the string is to be split by. In the code above, the string is split at the location of the hyphen. This creates an array in "$split" with two elements. The first element contains all the text up to the hyphen and the second element contains all the text after the hyphen. The contents of the two elements are then displayed to verify the command has worked.
Run the script in PowerShell by typing ".\split.ps1" and the display will show the output below, indicating the command was successful:
Windows PowerShell Hello World!