If I want to run a powershell script from the same folder where it exists what should I use ?? In batch script i generallly do the following method 


if not exist C:\install\Logs\ md C:\install\Logs\
"%~dp0setup.exe" /L*v "c:\install\logs\setup.log"
) else (
"%~dp0setup.exe" /L*v "c:\install\logs\setup.log"


Answer Summary:
0 Comments   [ - ] Hide Comments


Please log in to comment

Community Chosen Answer



This should do the trick:

$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Answered 07/19/2012 by: Ifan
Second Degree Green Belt

Please log in to comment
Answer this question or Comment on this question for clarity



$0 = $myInvocation.MyCommand.Definition

from there, we can get

$dp0 = [System.IO.Path]::GetDirectoryName($0)

Answered 07/19/2012 by: jagadeish
Red Belt

Please log in to comment

I have created a powershell script and you can use its function Get-ScriptDirectory to get the current folder and then use that folder as a variable and append it before setup.exe

You can get the script here.


You can join my blog/follow for more interesting stuff to come.

Answered 07/18/2012 by: piyushnasa
Red Belt

  • Hey, you should really try to get some exitcode handling in that script. Also, you can use the code:
    $install = start-process -filepath "msiexec.exe" -argumentlist '/i "foo bar 1.0.msi" /qb!' -wait -passthru

    instead of [Diagnostics.Process] to launch your setup. Easier to read and use unless you are a .NET Developer.

    Just type $install | get-member and you'll see ;)
Please log in to comment