0

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 current folder then drill down to Files.

I am trying to use something like this but its not working

Start-Process -FilePath '.\Files\setup.exe' -ArgumentList "-s" -Wait
Josh D'Ambrosio
  • 265
  • 1
  • 3
  • 10
  • possible duplicate of [What's the best way to determine the location of the current PowerShell script?](http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script) – briantist Jul 14 '15 at 03:45
  • I tried to get this to work but new to powershell so maybe I am doing something wrong but I couldnt get it to work – Josh D'Ambrosio Jul 14 '15 at 03:56
  • Edit your question to show the code you are trying. – briantist Jul 14 '15 at 03:57
  • There is a difference between the working directory and the directory where the script resides. `.` refers to the current working directory, but you want to refer to the script directory. That question has been answered here before though (see the link in my first comment). – briantist Jul 14 '15 at 04:06
  • I actually dont want to refer to the script directory. For example here is my folder structure. Fodler1\Script.ps1 But I want to reference Folder1\Files\(exefile here) – Josh D'Ambrosio Jul 14 '15 at 04:09
  • It would have been more accurate for me to say that you want to reference a directory *relative* to the script directory, rather than relative to the current working directory. The point still stands though; dot `.` does not do what you want it to. The linked question has the answer you need. – briantist Jul 14 '15 at 04:12
  • Like I said before that answer does not work for me as that seems to be for calling a script. I cannot get that to work for executing an exe via the start-process cmd – Josh D'Ambrosio Jul 14 '15 at 04:18

1 Answers1

1

The $PSScriptRootvariable will see you right if you're using PowerShell 3 or newer. In version 2 that variable would only work within modules, and I'm not sure about version 1 (before my time.)

$PSScriptRoot gives you the path of the directory in which the script is saved (not the full path including the file name so you don't need to worry about splitting that out.)

For your specific example:

Start-Process -FilePath "$PSScriptRoot\Files\setup.exe" -ArgumentList "-s" -Wait
Windos
  • 1,796
  • 1
  • 13
  • 19