Understanding PowerShell and System Paths

by Jul 20, 2017

PowerShell maintains its own current location:

 
PS> Get-Location

Path          
----          
C:\Users\tobwe
 

The current location applies to relative paths used with any cmdlet.

 
PS> Get-Location

Path          
----          
C:\Users\tobwe



PS> Resolve-Path -Path .

Path          
----          
C:\Users\tobwe 
 

There is another current path, maintained by Windows, that applies to all .NET methods. It may be different than PowerShell’s current path:

 
PS> [Environment]::CurrentDirectory
C:\test

PS> [System.IO.Path]::GetFullPath('.')
C:\test

PS>  
 

So if you are using .NET methods in your script that are related to file paths, you may want to sync both paths first. This line makes sure .NET uses the same file system path that is used by PowerShell:

 
PS> [Environment]::CurrentDirectory = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation
 

After you synced, cmdlets and .NET methods work on the same path:

 
PS> [Environment]::CurrentDirectory = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation

PS> [System.IO.Path]::GetFullPath('.')
C:\Users\tobwe
 
PS> Resolve-Path '.'

Path          
----          
C:\Users\tobwe 
 

Twitter This Tip! ReTweet this Tip!