Is there a powershell cmdlet equivalent to [System.IO.Path] :: GetFullPath ($ fileName); when $ fileName does not exist?

If $ fileName exists, the equivalent of cmdlet [System.IO.Path]::GetFullPath($fileName); (Get-Item $fileName).FullName . However, an exception occurs if the path does not exist. Is they another cmdlet that I don't see?

Join-Path unacceptable because it will not work when passing an absolute path:

 C:\Users\zippy\Documents\deleteme> join-path $pwd 'c:\config.sys' C:\Users\zippy\Documents\deleteme\c:\config.sys C:\Users\zippy\Documents\deleteme> 
+4
source share
3 answers

Join-Path would be a way to get the path to a nonexistent element that I consider. Something like that:

 join-path $pwd $filename 

Update:

I do not understand why you do not want to use .Net code. Powershell is .Net. All cmdlets are .Net code. The only valid reason for this is that when using .NET Code, the current directory is the directory from which Powershell was run, not $pwd

I simply list the ways in which I believe that this can be done so that you can deal with absolute and relational paths. None of them look simpler than GetFullPath() :

 $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($filename) 

If you are concerned about the lack of an absolute path or not, you can do something like:

 if(Split-Path $filename -IsAbsolute){ $filename } else{ join-path $pwd $filename # or join-path $pwd (Split-Path -Leaf $filename) } 

It's ugly

  $item = Get-Item $filename -ea silentlycontinue if (!$item) { $error[0].targetobject } else{ $item.fullname } 

A similar question, with similar answers: Powershell: allow a path that might not exist?

+8
source

You can use the Test-Path cmdlet to verify that it exists before receiving the full name.

 if (Test-Path $filename) {(Get-Item $fileName).FullName} 

EDIT:

Just your comment above on Test-Path is the equivalent of the function [system.io.file] :: exists (), and I believe that I now understand your question better.

There is no answer, as I see it, but you can make your own.

 function Get-Fullname { param($filename) process{ if (Test-Path $filename) {(Get-Item $fileName).FullName} } } 

you could tidy it up by indicating that the parameters accept the pipeline, both strings and properties, but that serves the purpose.

+1
source

You can remove the drive qualifier 'c: \ config.sys' (using Split-Path) and then join in two ways:

 PS > Join-Path $pwd (Split-Path 'c:\config.sys' -NoQualifier) C:\Users\zippy\Documents\deleteme\config.sys 
-1
source

All Articles