aasenomad
aasenomad

Reputation: 459

How to split a string correctly in PowerShell

I have this string

"C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online\_vti_history\101376\Shared Documents\Global PKI\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx"

I'm just wondering how can I split from \_ and get the result only like this

vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx

I've try using -split("\_") but it didn’t work.

$Path = "C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online\_vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx"

$Result = $Path -split('\_')[2]

any help or suggestion would be really appreciated.

Upvotes: 0

Views: 566

Answers (2)

js2010
js2010

Reputation: 27443

There are several things wrong. Because of high operator precedence with [2], you're splitting on $null, since ('\_') only has 2 characters. The parentheses are unnecessary but harmless, since -split is not a function. Splitting on '\_' would give a regex parsing error, so the backslash has to be escaped. Splitting on '(\\_)', with the parens inside the quotes, would include the divider in the result.

$Path = 'C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online\_vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx'


($Path -split '\\_')[1]

vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx


$Path -split '\_'

parsing "\_" - Unrecognized escape sequence \_.
At line:1 char:1
+ $Path -split '\_'
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException


$Path -split '(\\_)'

C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online
\_
vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx


'\_'[2] -eq $null

True

You can use one of the string.split .net function overloads where a string array is the first argument (not character array), but it requires a second argument. Then you don't have to worry about escaping regex characters. It's easier with a single character seperator.

# string[] Split(string[] separator, System.StringSplitOptions options)

$path.split([string[]]'\_','None')
# $path.split((,'\_'),0)

C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online
vti_history\101376\Shared Documents\Global JLN\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx

See all the overloads:

$path.split

OverloadDefinitions
-------------------
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

Upvotes: 1

Saddam Hussain I H
Saddam Hussain I H

Reputation: 200

You can do this

$myString = "C:\Users\testinguser\OneDrive - Company\Desktop\Hello\Jobs\Testing-Online\_vti_history\101376\Shared Documents\Global PKI\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx" -split "(\\_)"
Write-Output $myString[2]

Output : vti_history\101376\Shared Documents\Global PKI\Legacy Stuff\Vehicle Credential Trackers\Tracker_JLN_records_USE.xlsx

Upvotes: 2

Related Questions