Reputation: 531
I need to append a directory to a unc path, i need the following
\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\
To look like
\\10.0.0.1\share\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\
I'm having a hard time figuring out how to add a string after the first \\ in the UNC path.
Upvotes: 0
Views: 102
Reputation: 437111
A pragmatic solution is to do simple string concatenation, given that duplication of \
in paths is benign (multiple \
are treated as a single \
; try Get-ChildItem C:\\Windows
, for instance).
$uncShare = '\10.0.0.1\share'
$path = '\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch'
# Works fine.
"${uncShare}${path}"
# Ditto
Join-Path $uncShare $path
Upvotes: 0
Reputation: 25001
You can use regex replace to accomplish this:
"\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\" -replace "^\\",'\\10.0.0.1\share'
You could also use Join-Path as Olaf has suggested:
Join-Path -path "\\10.0.0.1\share" -ChildPath "\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\".TrimStart('\')
Both of the above solutions work if your child path is in a variable as well:
$Path = "\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\"
$Path -replace "^\\",'\\10.0.0.1\share'
Or:
Join-Path -Path "\\10.0.0.1\share" -ChildPath $Path.TrimStart('\')
Upvotes: 1