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

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 499194 Comments
Status Closed Workarounds
Type Bug Repros 9
Opened 10/21/2009 11:50:26 AM
Access Restriction Public


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.
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?