There are several ways you can get the current directory where your PowerShell script or cmdlets are being executed. You can get this information using (Get-Location).path or ($pwd).path or for the latest PowerShell version using the $PSScriptRoot environment variable. Below is one example for your reference –
1 2 3 4 5 6 7 8 9 10 |
## Create a powershell script @' echo "current path where script is being executed: $((Get-Location).path)" echo "another example of current path: $(($pwd).path)" echo "yet another example of current path: $((Get-Item .).FullName)" echo "final one for latest version: $PSScriptRoot" '@ > myscript.ps1 ## Execute the PowerShell file .\myscript.ps1 |