Use the Set-Location Cmdlet to Change the Working Directory in PowerShell. The Set-Location cmdlet similarly sets the working directory to a specified location like its legacy counterpart cd and chdir .
Yes, that should work. But if you need to see the absolute path, this is all you need:
(Get-Item .).FullName
The reliable way to do this is just like you showed $MyInvocation.MyCommand.Path
.
Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.
PowerShell v3+:
Use the automatic variable $PSScriptRoot
.
The easiest method seems to be to use the following predefined variable:
$PSScriptRoot
about_Automatic_Variables
and about_Scripts
both state:
In PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in PowerShell 3.0, it is valid in all scripts.
I use it like this:
$MyFileName = "data.txt"
$filebase = Join-Path $PSScriptRoot $MyFileName
You can also use:
(Resolve-Path .\).Path
The part in brackets returns a PathInfo
object.
(Available since PowerShell 2.0.)
Try :
(Get-Location).path
or:
($pwd).path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With