• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Split Operator

PowerShell Split Operator

18/06/2018 by Stephanos Leave a Comment

PowerShell Split Operator

In another tutorial we saw how we are able to use Join operator and what we are able to do with it. In this tutorial we will look into PowerShell Split Operator, how we are able to use it and what we can do with it.

The split operator allows you to split a string or multiple strings into sub-strings based on a delimiter that you provide. If you do not specify and delimiter, the default one is white space (” “). Split operator by default will return all sub-strings. You are able to specify maximum number of sub-strings. If you specify a number that is less that the number of sub-strings, then the last sub-string will combine all the rest of sub-strings above that number.

The below examples show us the default behaviour of the split operator without specifying any delimiter other than the default.

Code:

-split "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
-split $a

Output:

PowerShell Split Operator - No Delimiter

As you can see above, the string does not matter if you save it in a variable or not. The string is split based on the default delimiter which is white space (” “).

[adinserter name=”In Article”]

Delimiter

On the next examples we will use delimiter in order to split our string into sub-strings. Each example is a different case with different result. On the first example, dash ( – ) is used as a delimiter to split the string. As you will see the delimiter is omitted from the output.

Code:

$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split "-"

Output:

PowerShell Split Operator - With Delimiter

If you want to keep the delimiter in the output when you will use -split , then you need to enclose it in parentheses ( ). The below examples will show how this can be done.

Code:

$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split "(-)"

Output:

PowerShell Split Operator - Keep Delimiter

As you can see above, we are able to keep the delimiter in the output. There are some cases that we might need to use more that one character as a delimiter and keep only part of it in our output. If you want to keep only part of a delimiter, then you need to enclose only that part in parentheses ( ).

Code:

$a = "Monday:-:Tuesday:-:Wednesday:-:Thursday:-:Friday:-:Saturday:-:Sunday"
$a -split ":(-):"
$a = "Monday:-Tuesday:-Wednesday:-Thursday:-Friday:-Saturday:-Sunday"
$a -split ":(-)"

Output:

PowerShell Split Operator - Partial Delimiter

[adinserter name=”In Article”]

Max-Strings

As it was mentioned before the default behaviour of -split operator is to show all sub-strings if it is not specified differently. The below examples use max sub-strings on the output. The first example will not keep the delimiter in the output and the second examples will keep the delimiter in the example.

 

Code:

$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split "-" , 5
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split "(-)" , 5

Output:

PowerShell Split Operator - Max Substrings

[adinserter name=”In Article”]

Multiple Strings

If you have multiple strings that you need to split them, you need to use a specific format in order to split both strings. The below explanation is as provided by Microsoft.

The unary split operator (-split <string>) has higher precedence than a comma. As a result, if you submit a comma-separated list of strings to the unary split operator, only the first string (before the first comma) is split.

Please check below examples:

Code:

$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
$b = "1 2 3 4 5 6 7"
-split $a,$b

Output:

PowerShell Split Operator - Multi - OnlyOne

Code:

$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
$b = "1 2 3 4 5 6 7"
$a , $b -split " "
$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday","1 2 3 4 5 6 7"
-split $a
$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
$b = "1 2 3 4 5 6 7"
-split ($a,$b)

Output:

PowerShell Split Operator - Multi - All

[adinserter name=”In Article”]

RegexMatch Split

You are also able to split a string based on conditions. This can be useful if you need to use multiple delimiters or if you want to proceed split the string differently under specific conditions. Lets check the below examples.

Code:

$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split {$_ -eq "d"}
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday"
$a -split {($_ -eq "e") -or ($_ -eq "d")}

Output:

PowerShell Split Operator - Regex

As you can see above you are able to have a regex for delimiters. Please note that you are only able to use only one character in order to work.

Code:

$x = 3
$a = "Monday-Tuesday:Wednesday-Thursday:Friday-Saturday:Sunday"
$a -split {if ($x -gt 4) {$_ -eq "-"} else {$_ -eq ":"}}
$x = 5
$a = "Monday-Tuesday:Wednesday-Thursday:Friday-Saturday:Sunday"
$a -split {if ($x -gt 4) {$_ -eq "-"} else {$_ -eq ":"}}

Output:

PowerShell Split Operator - Condition

As you can see above you are able to have statements in order to specify a delimiter based on specific conditions. In the above examples we are checking the value of $x in order to specify the delimiter. If the value of $x is more than 4 then the delimiter is - else the delimiter is :.

I hope the tutorial about PowerShell Split Operator is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

  • PowerShell Join Operator
  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Split | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Split Operator
Article Name
PowerShell Split Operator
Description
PowerShell Split Operator. In this tutorial you will see how you are able to use and what you can do with the split operator in PowerShell.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: PowerShell Operators, PowerShell Split

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact