Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Here is a UNC path I've got.\\sharespace\test1\10.0.1212.1

Can I get its parent path\\sharespace\test1\?

share|improve this question
    
Basic powershell questions are more likely to be tolerated and answered well on stackoverflow.com . –  Mark Aug 4 '11 at 19:58

3 Answers 3

up vote 4 down vote accepted

There are a few options. If you have access to the share then:

(Get-Item '\\sharespace\test1\10.0.1212.1').Parent.Fullname

If you do not have access to the share, you can go the more unpalatable route of manipulating the string in various ways. This is one:

$path = '\\sharespace\test1\10.0.1212.1'
[string]::Join('\', $path.Split('\')[0..$($path.Split('\').Length-2)])
share|improve this answer
    
Thanks very much! –  pansal Aug 5 '11 at 2:57

Here's a simple way to parse the parent path out of the full UNC path.

$fullPath = "\\sharespace\test1\10.0.1212.1"
$parentPath = "\\" + [string]::join("\",$fullPath.Split("\")[2..3])
share|improve this answer

There's a cmdlet for this:

Split-Path \\sharespace\test1\10.0.1212.1
\\sharespace\test1

It doesn't include the trailing '\', but if you use Join-Path then PowerShell will take care of adding it if required anyway.

NOTE: Split-Path and Join-Path try to resolve the path on the local machine, so if the path has a drive letter (say, 'F:') that is not a valid drive on the local machine, these cmdlets will throw an error. With UNC paths like the above you shouldn't have a problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.