Home Dashboard Directory Help
Search

Split-Path gets error if drive doesn't exist locally by Stephen Mills


Status: 

Closed
 as Fixed Help for as Fixed


10
0
Sign in
to vote
Type: Bug
ID: 499194
Opened: 10/21/2009 11:50:26 AM
Access Restriction: Public
0
Workaround(s)
view
9
User(s) can reproduce this bug

Description

If you have a path that you want to split that was generated on a remote system, Split-Path will generate an error unless you have the same drive letter on your local system. If you leave out the drive letter and ":", then it works as expected, except you no longer have a reference to the drive letter. Since it doesn't validate any other part of the path, it shouldn't validate the drive letter existing either.
Details
Sign in to post a comment.
Posted by SadMacc on 2/17/2013 at 12:32 PM
I can reproduce the problem in Powershell 2.0:

Split-Path : Cannot find drive. A drive with the name 'D' does not exist.
    + CategoryInfo         : ObjectNotFound: (D:String) [Split-Path], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SplitPathCommand

This is strange, I assumed Split-Path didn't hit the file system, and got the error above.

An ugly but robust work-a-round is to use [System.IO.Path]::GetDirectoryName, or [System.IO.Path]::GetFileName if you're using the -Leaf option. Those functions don't hit the local file system, and I assumed Split-Path used these functions internally.

This is a dangerous bug which can surprise everyone when you least expect it. Even when using a single computer the drive configuration can change.

Lesson: Don't use Split-Path in ANY powershell code, use the [System.IO.Path] functions instead. Can we have some feedback if this will be fixed in Powershell 2.0 at all?
Sign in to post a workaround.