Powershell equivalent of DOS %~dp0 command

Hello Folks!

Does anyone know of a way to output the directory path of the powershell file being executed is located?  I'm using SCCM to install applications via a script which means the source files (inlcuding the script) will be ran from multiple distribution points and I don't want to point to one particular server's source files.

In Dos or Batch scripts this is done by %~dp0filename.msi.  Is there a way to do this via Powershell?

Thanks in adavance!!

February 26th, 2014 8:30pm

If you're using PowerShell version 3.0 or later, this is in an automatic variable called $PSScriptRoot.

In PowerShell 2.0, that variable didn't exist, but you could get at the same information through $MyInvocation.  For example:

$scriptRoot = Split-Path -Path $MyInvocation.MyCommand.Path

  • Marked as answer by NeedAnswers8 Wednesday, February 26, 2014 8:47 PM
Free Windows Admin Tool Kit Click here and download it now
February 26th, 2014 8:34pm

Thank you David!!  That did the trick! I'm using 2.0, but we'll soon be upgrading to 3.0 so both are handy.  Thanks again!
February 26th, 2014 8:47pm

To add to this, the $PScriptRoot variable does not include the trailing "\" like %~dp0. It has to be added to the path. For example, xcopy $PSScriptRoot\*.* /s /e "C:\Program Files\OpsManModule"

-Tony

Free Windows Admin Tool Kit Click here and download it now
June 12th, 2015 6:00pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics