Reputation: 13
I'm trying to gather a report of long directory paths to provide to each user who has them so that they can use it and make this folders paths short.
How can I replace \\server\Share$ to X: ? I tried the below but nothing changes. I can only get results if I do only one character or one string "\\server" but not the combination "\\server\Share$" can someone tell me what I'm doing wrong.
$results= "\\\server\Share$\super\long\directory\path\"
$usershare="\\\server\Share$"
$Results | ForEach-Object { $_.FullName = $_.FullName -replace "$usershare", 'X:' }
The output I need is which is what the users will see in their systems.
X:\super\long\directory\path\
Upvotes: 1
Views: 738
Reputation: 61028
Because the $userShare
variable contains characters that have special meaning in Regular Expressions (and -replace
uses Regex), you need to [Regex]::Escape()
that string.
First thing to notice is that you start the UNC paths with three backslashes, where you should only have two.
Next is that your $results
variable is simply declared as string and should probably be the result of a Get-ChildItem
command..
I guess what you want to do is something like this:
$uncPath = "\\server\Share$\super\long\directory\path\" #"# the UNC folder path
$usershare = "\\server\Share$"
$results = Get-ChildItem -Path $uncPath | ForEach-Object {
$_.FullName -replace ([regex]::Escape($usershare)), 'X:'
}
Hope that helps
Upvotes: 4