Skip to main content
TopMiniSite

Back to all posts

How to Split String By String In Powershell?

Published on
3 min read
How to Split String By String In Powershell? image

Best PowerShell Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
4 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$50.16
Learn PowerShell Scripting in a Month of Lunches
5 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
6 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
7 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
8 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
9 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
10 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
+
ONE MORE?

In PowerShell, you can split a string by another string using the Split method or the -split operator.

To split a string by a specific string using the Split method, you can use the following syntax:

$string.Split('separator')

To split a string by a specific string using the -split operator, you can use the following syntax:

$string -split 'separator'

Both methods will return an array of substrings that were separated by the specified separator string.

For example, if you have a string like "hello-world-123" and you want to split it by the "-" character, you can use the following code:

$string = "hello-world-123" $substrings = $string -split '-'

This will result in $substrings containing an array with three elements: "hello", "world", and "123".

What is the way to change the order of elements after splitting a string in PowerShell?

One way to change the order of elements after splitting a string in PowerShell is to store the result of the split operation in a variable, manipulate the elements in that variable, and then join them back together. Here's an example:

$string = "apple,banana,cherry" $elements = $string -split "," $reversedElements = $elements | Select-Object -Last $($elements.Count) -First 1 -Skip 0 $reversedString = $reversedElements -join ","

Write-Output $reversedString

In this example, we first split the original string into an array of elements using -split ",". Then, we use the Select-Object cmdlet to reorder the elements in the array by selecting -Last $($elements.Count) -First 1 -Skip 0, effectively reversing their order. Finally, we join the elements back together using -join "," to get the reversed string.

What is the variable assignment method after splitting a string in PowerShell?

After splitting a string in PowerShell, you can assign the parts of the split string to variables using the following syntax:

$var1, $var2, $var3 = $string.Split('delimiter')

In this syntax, $string is the original string that you want to split, 'delimiter' is the character or substring that you want to use as the delimiter for splitting the string, and $var1, $var2, and $var3 are the variables to which you want to assign the parts of the split string. The number of variables should match the number of parts resulting from the split operation.

How to split a string and remove empty entries in PowerShell?

To split a string and remove empty entries in PowerShell, you can use the -split operator along with the -ne operator to remove empty entries. Here's an example:

$string = "apple,banana,,orange,,"

Split the string by comma and remove empty entries

$splitString = $string -split ',' $filteredString = $splitString | Where-Object { $_ -ne '' }

Output the filtered string

Write-Output $filteredString

In this example, we first define a string with multiple entries separated by commas. We then use the -split operator to split the string into an array based on the comma delimiter. Finally, we use the Where-Object cmdlet with the -ne operator to filter out any empty entries in the array.