I'm seeing some inconsistent behaviour in PowerShell Core 7.3 with variables of type [System.IO.DirectoryInfo] - the following works as expected:
$dir = [System.IO.DirectoryInfo]'c:\deploy'
$dir.BaseName # returns 'deploy'
$dir.Name # returns 'deploy'
$dir.Parent.Name # returns 'c:\'
But where the assignment is a UNC share, the behaviour isn't as expected:
$target = [System.IO.DirectoryInfo]'\\sharedhost\Deploy'
$target.Name # returns '\\sharedhost\Deploy'
$target.BaseName # returns '\\sharedhost\Deploy'
$target.Parent.Name # returns nothing, checking shows that Parent is null
I would expect $target.Parent to be 'sharedhost' as that's the parent of the Deploy folder, but instead it returns $null.
I suppose I could temporarily map a drive to that share and use that mapping but that's a faff and besides drive-mapping might not be a robust option.
Can anyone please explain why DirectoryInfo doesn't appear to work as expected with UNC paths?
Your expectations are wrong. If you map
\\sharedhost\Deployto a drive, you will see that neithersharedhostnorDeploywill be a parent of it.It is just how UNC paths are defined:
In your case:
sharedhostis the hostname, not a (parent) directoryDeployis the sharename, not a directory1Everything else underneath
\\sharedhost\Deploywill be files and directories. So consider\\sharedhost\DeploylikeC:\in your thinking.1
Deploymay exist as a directory on the file server, but the share does not have to have the same name as the shared directory.