Linked Questions
33 questions linked to/from What's the best way to determine the location of the current PowerShell script?
65 votes
7 answers
94k views
How can I find the source path of an executing script? [duplicate]
I want to be able to tell what path my executing script was run from. This will often not be $pwd. I need to call other scripts that are in a folder structure relative to my script and while I could ...
67 votes
1 answer
160k views
How do I get the directory of the PowerShell script I execute? [duplicate]
I run a PowerShell script. How do I get the directory path of this script I run? How to do this?
71 votes
4 answers
129k views
Path of currently executing powershell script [duplicate]
How do I do this in PowerShell. In a batch file I would do: %~d0%~p0
2 votes
1 answer
5k views
Using current directory as output file for Powershell [duplicate]
I am trying to have powershell spit out the output file from a command to the current folder as I am trying to create a printer migration that can be run from a USB stick. My command is simply: C:\...
2 votes
0 answers
4k views
use relative path for file in powershell [duplicate]
I am using a PowerShell file. I have a file inside the same folder, path can change but folder remains same. How to use relative path, so there is no change in script? Param([string]$servername, [...
2 votes
0 answers
587 views
Difference between $MyInvocation and $PSScriptRoot [duplicate]
Edited to better explain overall question: In PowerShell, what is the difference between using $MyInvocation.MyCommand.Path and $PSScriptRoot for acquiring the running script's current root directory,...
286 votes
20 answers
572k views
How to get the current directory of the cmdlet being executed
This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute C:\temp\...
68 votes
4 answers
106k views
How can I get the file system location of a PowerShell script?
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 ...
9 votes
3 answers
16k views
Functional differences between $PSScriptRoot and $MyInvocation
Problem I am working with Jenkins to deploy PowerShell scripts remotely. As such, I am trying to figure out if there will be problems utilizing $PSScriptRoot over $MyInvocation.MyCommand.Path for ...
10 votes
1 answer
25k views
How to find location/path of current script in powershell
I have a script (script1.ps1) that lies in C:\FolderA What powershell code can I insert in the script, that it will print what ever current location/path of the script. (The goal is to make this ...
9 votes
5 answers
17k views
PowerShell: Run script from shortcut using relative path
EDIT: To future readers, in short, PowerShell scripts weren't intended to be used this way which is why there is no elegant solution. I have the following line which runs a script as an administrator ...
3 votes
1 answer
13k views
How can I specify a path relative to where my script is running? [duplicate]
I have this: Get-ChildItem c:\H\admin\contents Is it possible to do the same thing but relative to the folder that the powershell script runs in. Sometimes I have the script running in c\H\admin ...
3 votes
1 answer
3k views
Powershell $MyInvocation.MyCommand.Path returns null when convert script in exe
I created my ps1 script and to get the path dynamically I used this command $ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent Everything works fine when I use ISE console but ...
2 votes
3 answers
2k views
Script location of a remotely executed script?
How can I get a remotely executed script to know it's own location? I'm using Invoke-Command to run a script on a remote server. The script needs to create files in the directory in which it lives. ...
0 votes
1 answer
5k views
Calling file inside script folder in PowerShell
I have a powershell script in say \Folder\ I then have an exe that I am trying to execute from \Folder\Files\ How can I execute that file without having to specify the whole path and just use either ...