Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslashes in Powershell

Why does the string for the -split parameter require two backslashes while the string for the -join parameter requires only one backslash? The backtick is the escape character in Powershell. What does a 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

like image 839
lit Avatar asked Jun 23 '14 20:06

lit


People also ask

How do you write a backslash in PowerShell?

PowerShell uses backquote as the escape character. As a mnemonic, think of the backquote as a small backslash.

How do you escape a backslash in PowerShell?

Can you give us a bit more details about your code? (To escape the backslash you need to use double backslash : "\\" instead of "\".)

How do you escape special characters in PowerShell?

The PowerShell escape character is the backtick "`" character. This applies whether you are running PowerShell statements interactively, or running PowerShell scripts.

How do you use special characters in PowerShell?

Unicode character (`u{x}) This special character was added in PowerShell 6.0. The Unicode escape sequence ( `u{x} ) allows you to specify any Unicode character by the hexadecimal representation of its code point.


1 Answers

-split splits on a regex by default. So it is the regex that requires escaping a backslash. You can tell PowerShell not to use a regex like so:

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

-join is just taking whatever characters you supply to use as the delimiter to stick between the strings being joined.

like image 134
Keith Hill Avatar answered Oct 04 '22 10:10

Keith Hill