/build/static/layout/Breadcrumb_cap_w.png

Equivalent to %~dp0 in powershell???

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\
if %PROCESSOR_ARCHITECTURE%==x86 (
"%~dp0setup.exe" /L*v "c:\install\logs\setup.log"
) else (
"%~dp0setup.exe" /L*v "c:\install\logs\setup.log"
)

 


0 Comments   [ + ] Show comments

Answers (3)

Answer Summary:
Posted by: Ifan 12 years ago
Second Degree Green Belt
2

This should do the trick:

$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Posted by: jagadeish 12 years ago
Red Belt
1

$0 = $myInvocation.MyCommand.Definition

from there, we can get

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

Posted by: piyushnasa 12 years ago
Red Belt
0

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.

http://msiworld.blogspot.com.au/2012/01/my-first-powershell-script.html

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


Comments:
  • 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 ;) - Ifan 12 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ