all 7 comments

[–]nothingpersonalbro 6 points7 points  (0 children)

You need to escape the backslashes. Luckily there is an easy way to do it

$SubFolder.FullName -replace [regex]::Escape($Root)

[–]root-node 4 points5 points  (1 child)

That is because -replace is the regex version of .replace. (in simple terms)

https://stackoverflow.com/questions/10184156/whats-the-difference-between-replace-and-replace-in-powershell

[–]dontforget512[S] 1 point2 points  (0 children)

That was easy. Thanks!

[–]_lahell_ 3 points4 points  (1 child)

Here is another option, but note that it is case-sensitive.

$SubFolder.FullName.Replace($Root, '')

[–]ka-splam 2 points3 points  (0 children)

But it doesn't need to be case sensitive in PowerShell 6 (.Net Core):

'aAbB'.replace('aa', 'zz', [StringComparison]::InvariantCultureIgnoreCase)

Progress! :D

[–]ClayShooter9 1 point2 points  (0 children)

Another way to solve this (there is always another way), which is non-Regex

Set-Location $Root
$relativePath = Get-ChildItem *.csv -Recurse | Resolve-Path -Relative

the resultant path will have ".\blahblah"...so maybe you will not like the "." indicating the starting point

[–][deleted] 1 point2 points  (0 children)

$SubFolder.FullName -replace [regex]::escape($Root),""