How To Split Strings with PowerShell

One of PowerShell’s most powerful features is its ability to split strings. Split strings can be used for a variety of purposes, such as parsing log files and extracting data.

PowerShell split operator

A PowerShell split operator is used to split a string on one or more characters. PowerShell split operators are different from PowerShell delimiters, filters, and PowerShell wildcards, but they are still important PowerShell features.

When the operator split a string it splits it into what is called a substring.

Exmple

The following code examples will show you how to get started with the split operator and creating substring.

The most basic example is to split a string every time there is whitespace. By default, the operator will split a string when it sees a white space.

#Create new line everytime there is a whitespace 
-split "nt weekly"

Another common scenario is to split a string every time there is a dot. In the example below we are doing it but we also use extra parameters.

, 0 – This means we returning unlimited results (0). If we write 2 only two splits will happen. this option is called max-substring.

SimpleMatch – Tells the operator to look for a dot (.) and not for any character.

IgnoreCase – Tells the operator to split regardless of the character regardless if it is capital or not.

# Remove dot (.) from string 
$var= "address.corp.local"
$newvar = $var -split ".",0,"SimpleMatch","IgnoreCase"
$newvar

We can also use a script block and use conditions for splitting a string. in the code below a string is split only if there is a character is equal to p.

# Use script block 
$var2= "address corp local"
$newvar2 = $var2 -split {$_ -eq "p"}
$newvar2

The last code example uses the -csplit operator which is case sensitive.

# Use case-sensitive split 
$var3= "address Corp local"
$newvar3 = $var3 -csplit {$_ -eq "C"}
$newvar3

About PowerShell

PowerShell is a task-based command-line shell and scripting language that supports several types of operations, including synchronous and asynchronous running of PowerShell scripts, automation of system administration tasks, and interaction with PowerShell cmdlets. PowerShell was developed to be a convenient, user-friendly administrative tool for systems management.

Even though PowerShell is a powerful tool, it can take a while to learn how to use PowerShell commands in order to do specific tasks. For this reason, PowerShell scripts can be written to automate PowerShell commands, which enables you to perform repetitive or complex tasks by executing PowerShell commands via the script. This eliminates the need for repetitive steps and allows you to focus on the actions that require your input.

About PowerShell 7

PowerShell 7 is PowerShell that runs on Linux, macOS, and Windows PowerShell Core includes all the features of PowerShell 5.1, plus several new enhancements not found in PowerShell 5.1.

,

Processing…
Success! You're on the list.

Posted

in

,

by