EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Split String
 

PowerShell Split String

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

PowerShell Split String

 

 

Introduction to PowerShell Split String

PowerShell uses the Split () function to split a string into multiple substrings. The function uses the specified delimiters to split the string into sub strings. The default character used to split the string is the whitespace. Three types of elements are associated with the split function. They are delimiter, the maximum number of substrings and options related to delimiter, either SimpleMatch or Multiline.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Delimiter: The default delimiter is whitespace. Other delimiters such as patterns, tabs, and backspace can also be used.

Maximum number of Substrings: By default, the function returns all the possible substrings. We can also limit them using this element.

Syntax:

.Split(strSeparator [, MaxSubstrings] [, Options])
String -Split strSeparator [, MaxSubstrings] [, Options] String -Split {scriptblock} [, MaxSubstrings] -Split String

Parameters:

1. -String[] or String: It denotes the strings to be split from the actual input. Multiple strings can also be specified.

2. -Delimiter: This denotes the character that is used to identify the end of a substring. The default delimiter is whitespace including tab and a newline character. The delimiter character is by default omitted in all the substrings, to include them it must be enclosed within parenthesis.

3. -Max-SubStrings: It denotes number of times, the input substring must be split, i.e. the number of substrings that should be produced. By default, all the possible substrings are generated. If the substrings are more than the specified number, then the leftover substrings are appended to the final substring. Non-negative values are allowed, zero returns all the substrings.

4. -ScriptBlock: It denotes the rules for the delimiter. It is enclosed within the braces and must evaluate either to true or false.

5. -Options: Single line identify only the starting and ending of strings, Multiline identifies both the start and end of lines as well as strings.

The syntax for options is below and it can only be used when the Max-SubStrings parameter is used.

"SimpleMatch [,IgnoreCase]"
"[RegexMatch] [,IgnoreCase] [,CultureInvariant] [,IgnorePatternWhitespace] [,ExplicitCapture] [,Singleline | ,Multiline]"

The possible Regex options other than SingleLine and MultiLine are as follows:

  • RegexMatch: This is the default option. It uses a regular expression to evaluate the delimiter.
  • IgnoreCase: It doesn’t consider the case of the delimiter
  • CultureInvariant: Cultural differences in the language is ignored when evaluating the delimiter.

Examples of PowerShell Split String

Given below are the examples of PowerShell Split String:

Example #1

Input:

Write-Host "generating substring using split method"
$teststring="my name is vignesh- am from chennai"
Write-Host "splitting using - character"
$teststring -split "-"
$teststring="domainname\username"
Write-Host "Splitting using \ character"
$teststring -split "\\"
Write-Host "generating substring using space"
$string="string1 string2 strin3"
$string.Split("")
Write-Host "splitting using multiple separators"
$string="domain\systems-test"
$string.Split("\\").Split("-")

Output:

Powershell split string 1

Example #2

Input:

Write-Host "Welcome to the Split string demo"
Write-Host "Splitting using white space"
$string="My name is vignesh Krishnakumar"
$string.Split("")
Write-Host "Splitting the string using single delimiter"
$teststring="there was, a man, 100 years ago, who used to, kill thousands of people, on a regualr basis, for no reason"
write-host "The input is" $teststring
Write-Host "The above input will be split using , as below"
$teststring.Split(",")
Write-Host "splitting the string using multiple delimiters"
$teststring1="there was, a man, whose name was king rama. he was a good person. his wife, name was sita."
Write-Host "input string is" $teststring1
Write-Host "splitting the above input using , and ."
$teststring1.Split(",").Split(".")

Output:

using white space

Example #3

Input:

Write-Host "Welcome to the Split string demo"
Write-Host "Usage of Max Substrings"
$string= "Jan-Feb-Mar-Apr-May-June-July-Aug-Sep-Oct-Nov-Dec"
Write-Host "Input string is" $string
Write-Host " Splititng the string to max 4 substrinngs"
$string -split "-" , 4
Write-Host "including the delimiter character is substring"
$string -split "(-)" , 4

Output:

Powershell split string 4

Example #4

Input:

Write-Host "Welcome to the Split string demo"
Write-Host "Welcome to Regex tutorial"
$month="Jan-Feb-Mar-Apr-May-June-July-Aug-Sep-Oct-Nov-Dec"
Write-Host "Delimiter is n"
$month -split {$_ -eq "n"}
write-host "************"
Write-Host "Multiple regex expressions"
$month -split {($_ -eq "n") -or ($_ -eq "a")}
write-host "************"
Write-Host "Along with a numerical condition"
$test=5
$month -split {if ($test -gt 4) {$_ -eq "a"} else {$_ -eq "f"}}
write-host "************"
$month -split {if ($test -eq 4) {$_ -eq "z"} else {$_ -eq "f"}}
Write-Host "*********"
$month.Split("[nf]")
Write-Host "**********"

Output:

Powershell split string 5

Example #5

Input:

Write-Host "Welcome to the Split string demo"
Write-Host "Extracting only particular substring"
$teststring= "hello how are you am fine my name is vignesh krishnakumar am from chennai"
$months=$teststring.Split(" ")
Write-Host "First Word is" $months[0] Write-Host "third word is" $months[2] Write-Host "Seventh Word is" $months[6]

Output:

Powershell split string 6

Example #6

Input:

Write-Host "Welcome to the Split string demo"
Write-Host "*****************"
$input="sdfsd12323fgds567cxvdsfd12332"
Write-Host "The input is " $input
Write-Host "*****************"
Write-Host "Extracting numbers only from a string"
Write-Host "*****************"
$numbers= $input -split "\D"
Write-Host "extarcted numbers is " $numbers
Write-Host "*****************"
Write-Host "Extracting text only from a string"
Write-Host "*****************"
$numbers1= $input -split "\d"
Write-Host "extracted text is" $numbers1

Output:

Powershell split string 7

Example #7

Input:

This example will show how to split and generate substring based on regex and to split MAC addresses.

Write-Host "split using regex"
$test=" this . is an . example . of an was an and an . . . ."
$test.Split("an")
Write-Host "splitting a mac address"
$test="12-23-AB-DE-45-BC"
$test.Split("-")
Write-Host "Splitting an IP Address"
$test="122.43.56.78"
$test.Split(".")

Output:

 generate substring based on regex

Split-Path

The split path is used to return the mentioned part in a path. It can be a parent folder, a file name or a sub folder.

Syntax:

AME
Split-Path
Split-Path [-Path] <string[]> [-Parent] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>] Split-Path [-Path] <string[]> [-NoQualifier] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>] Split-Path [-Path] <string[]> [-Leaf] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>] Split-Path [-Path] <string[]> [[-Qualifier]] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]

Example:

Input:

Write-Host "Split Path example"
Write-Host "returning the drive of a path"
Split-Path -Path "C:\Users\R003646\Desktop\Articles\Jan" -Qualifier
Write-Host "Dispalying the files inside a folder"
Split-Path -Path "C:\Vignesh\Test\Test6\*.txt" -Leaf -Resolve

Output:

split path

Conclusion

Thus, the article is covered in detail about the split string method in PowerShell. It explained the various delimiters with appropriate examples. It also showed the various methods of generating substring using the split operation. It also explained briefly on splitting the path from a given path using the split path cmdlet.

Recommended Articles

This has been a guide to PowerShell Split String. Here we discuss the introduction, parameters, and different examples of Powershell split string. You may also have a look at the following articles to learn more –

  1. PowerShell Get-Content
  2. PowerShell ForEach Object
  3. PowerShell Operators
  4. PowerShell Rename-Item
  5. Guide to PowerShell Convert to String
  6. Guide to Python Split String

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW