Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split-Path with Root UNC Directory

So I've got a UNC path like so:

\\server\folder

I want to get just the path to server, eg \\server. Split-Path "\\server\folder" -Parent returns "". Anything I try which deals with the root, fails. For example, Get-Item "\\server" fails too.

How can I safely get the path of \\server from \\server\\folder in PowerShell?

like image 756
mamidon Avatar asked Aug 21 '13 18:08

mamidon


2 Answers

By using the System.Uri class and querying its host property:

$uri = new-object System.Uri("\\server\folder")
$uri.host # add "\\" in front to get exactly what you asked

Note: For a UNC path, the root directory is the servername plus the share name part.

like image 78
jon Z Avatar answered Sep 22 '22 23:09

jon Z


An example using regular expressions:

'\\server\share' -replace '(?<!\\)\\\w+'
like image 40
Shay Levy Avatar answered Sep 25 '22 23:09

Shay Levy