Powershell Backslashes

Why does the string for the -split parameter require two backslashes, and the string for the -join parameter needs only one backslash? The flyback is a symbol of escape in Powershell. What does the backslash preceding a character do for you?

$path = 'C:\folder\test\unit1\testing\results\report.txt' $path -split '\\' -notlike '*test*' -join '\' 

http://powershell.com/cs/blogs/tips/archive/2014/06/17/fun-with-path-names.aspx

+7
windows powershell quoting
source share
2 answers

-split is divided by regular expression by default. Therefore, regex requires faster backslashes. You can tell PowerShell not to use regex like this:

 $path -split '\',-1,'SimpleMatch' 

-join just accepts any characters you use as a delimiter to stick between the joined strings.

+9
source share

-split accepts a regular expression where the backslash is a special character, so it must be escaped

+1
source share

All Articles