Thursday 27 February 2014

Interesting Issues with Join-Path in PowerShell - Brain Dump 4

Another post part of the brain dump series

It turns out that Join-Path can't be used reliably for remote paths, allow me to explain

This will work fine:
PS C:\Users\John> Join-Path "\\SecretMachine\E$\SecretDirectory" "ReallySecretDirectory"
\\SecretMachine\E$\SecretDirectory\ReallySecretDirectory
But this won't:
PS C:\Users\John> Join-Path "E:\SecretDirectory" "ReallySecretDirectory"
Join-Path : Cannot find drive. A drive with the name 'e' does not exist.
At line:1 char:10
+ Join-Path <<<<  "e:\SecretDirectory" "ReallySecretDirectory"
    + CategoryInfo          : ObjectNotFound: (e:String) [Join-Path], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

Essentially, Join-Path can handle non-existent UNC paths but if it looks like a local path it expects it to be there.

You can force Join-Path to try to resolve the UNC path but it doesn't look like you can prevent it for local paths.
PS C:\Users\John> Join-Path "\\SecretMachine\F$" "SecretDirectory" -Resolve
Join-Path : Cannot find path '\\SecretMachine\F$\SecretDirectory' because it does not exist.
At line:1 char:10
+ Join-Path <<<<  "\\SecretMachine\F$" "SecretDirectory" -Resolve
    + CategoryInfo          : ObjectNotFound: (\\SecretMachine\F$\SecretDirectory:String) [Join-Path], ItemNotFoundExc
   eption
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

2 comments:

  1. I've faced the same issue and this is ridiculous! I've always assumed that Join-Path is a handy way to glue paths and don't mess with slashes manually. I was wrong. Moreover, I hit this bug: http://connect.microsoft.com/PowerShell/feedback/details/781816/join-path-cannot-find-drive-a-drive-with-the-name-e-does-not-exist and it makes Join-Path completely unusable in my case. What a day!

    ReplyDelete
  2. Same issue in powershell 5.0

    ReplyDelete