I have a PowerShell script located at D:\temp
.
When I run this script, I want the current location of the file to be listed. How do I do this?
For example, this code would accomplish it in a DOS batch file; I am trying to convert this to a PowerShell script...
FOR /f "usebackq tokens=*" %%a IN ('%0') DO SET this_cmds_dir=%%~dpa
CD /d "%this_cmds_dir%"
Roman Kuzmin answered the question imho. I'll just add that if you import a module (via
Import-Module
), you can access$PsScriptRoot
automatic variable inside the module -- that will tell you where the module is located.PowerShell 3+
The path of a running scripts is:
Its directory is:
PowerShell 2
The path of a running scripts is:
Its directory is: