EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Split String

By Priya PedamkarPriya Pedamkar

Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Split String

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.

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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]"

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,938 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - PowerShell Training (2 Courses) Learn More