66

I run a PowerShell script. How do I get the directory path of this script I run?

How to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2131116
  • 2,761
  • 6
  • 26
  • 33
  • do you want current working directory of the process? – Bill Jul 04 '13 at 03:09
  • 1
    No , I want to know which directory contain this script . – user2131116 Jul 04 '13 at 03:20
  • if you know the filename, you can use `get-childItem` to find its path. http://stackoverflow.com/questions/8677628/recursive-file-search-using-powershell – Bill Jul 04 '13 at 03:21
  • I use get-childItem to get its path , but the result seems contains too much infomation but not just only the path ... – user2131116 Jul 04 '13 at 03:28
  • you need to use some filtering. look at the link i posted in my earlier comment. that might be useful. – Bill Jul 04 '13 at 03:34

1 Answers1

173

PowerShell 3 has the $PSScriptRoot automatic variable:

Contains the directory from which a script is being run.

In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

Don't be fooled by the poor wording. PSScriptRoot is the directory of the current file.

In PowerShell 2, you can calculate the value of $PSScriptRoot yourself:

# PowerShell v2
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
  • 16
    Why the down vote? Help me make my answer better. Please provide feedback! – Aaron Jensen Jul 04 '13 at 04:06
  • 2
    I'm a bit new to powershell, could you expand on $MyInvocation.MyCommand.Definition? I see this in many posts and it seems like a placeholder, but for what? – Sinaesthetic Dec 23 '13 at 19:37
  • Agreed: is 'MyCommand.Definition' something I have to type or replace with my script name ? I just tried at the command prompt, and I get nothing back from this ? – monojohnny Mar 03 '16 at 15:20
  • 3
    @monojohnny $MyInvocation isn't available at the console, just inside functions and scripts. – Aaron Jensen Mar 03 '16 at 20:04
  • 1
    @Sinaesthetic It isn't a placeholder. $MyInvocation is a [System.Management.Automation.InvocationInfo object](https://msdn.microsoft.com/en-us/library/system.management.automation.invocationinfo.aspx) representing the context the current code was called in. It has all sorts of metadata attached to it. – Aaron Jensen Mar 03 '16 at 20:09
  • Thanks both - I see it works just fine when used from inside a script, thanks ! – monojohnny Mar 04 '16 at 00:53
  • @DevisLucato The PowerShell documentation is worded poorly. It sounds like `PSScriptRoot` points to the user's current directory, but in fact `PSScriptRoot` is the directory of the current file. – Aaron Jensen Apr 13 '17 at 20:45
  • 1
    A possible 'gotcha', is that if your main script dot-sources scripts on different paths, then $PSScriptRoot will represent the path of the dot-sourced script, whilst that script it executes. – andyb Jul 06 '17 at 00:05