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
7
  • possible duplicate of What's the best way to determine the location of the current PowerShell script? Commented Jul 14, 2015 at 3: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 Commented Jul 14, 2015 at 3:56
  • Edit your question to show the code you are trying. Commented Jul 14, 2015 at 3: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). Commented Jul 14, 2015 at 4: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) Commented Jul 14, 2015 at 4:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

$PSScriptRoot was introduced with the support for modules in V2. So no, it wasn't available in V1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.