locked
Use Start-Process to open a PowerShell window and execute a command RRS feed

  • Question

  • I would like to use Start-Process to

    a. open a PowerShell window (and keep it open) (WORKS)

    b. change to a specific directory (WORKS)

    c. execute a command to bring up a vagrant box (HOW TO?)

    I don't know which option to include in the Start-Process command to execute the Vagrant command to bring the box up. (ie. 'vagrant up')

    What I have so far, opens a Powershell window and changes to a specific directory (location of Vagrant box).

    This is what I have so far. (both work)

    Start-Process powershell -LoadUserProfile -WorkingDirectory "K:\docker_class" 

    OR 

    Start-Process -FilePath "powershell" -LoadUserProfile -WorkingDirectory "K:\docker_class"

    Note, I am logged in as the Administrator on my Windows 7 SP1 laptop (PowerShell 5)


    • Edited by EarlPearl123 Thursday, January 9, 2020 12:54 PM cannot increase font size of question
    Thursday, January 9, 2020 12:52 PM

All replies

  • Write a script that executes the commands you want to run and start PowerShell with the script as an argument.

    Start-Process -FilePath powershell  -WorkingDirectory K:\docker_class -ArgumentList 'yourscript.ps1'


    \_(ツ)_/

    Thursday, January 9, 2020 1:00 PM
  • I ran the following:

    Start-Process -FilePath "powershell" -WorkingDirectory "K:\docker_class" -ArgumentList 'vagrant_up.ps1' 

    (vagrant_up.ps1 contains: Write-host "Testing 1, 2, 3")

    a new PS window opens, then closes quickly with an error message but I cannot read it.

    So, I ran: 

    ***NOTE: both 'create_docker_env.ps1' and 'vagrant_up.ps1' in same directory

    powershell -noexit create_docker_env.ps1 *> log_file.txt

    Hoping that redirecting all streams to the same file would at least give me
    something. But, the command just hangs, and log_file.txt is empty.
    Thursday, January 9, 2020 7:31 PM
  • First learn PowerShell then you would not need to do so much fishing around. Add the full path to the script name.


    \_(ツ)_/

    Thursday, January 9, 2020 9:16 PM
  • That worked. The Vagrant box is up. Thanks. 

    I would've liked the PS window to stay open after completing the 'vagrant_up.ps1' script.

    Is that possible? 

    What I've done instead is added another Start-Process command to my script:

    Start-Process -FilePath "powershell" -WindowStyle Normal -WorkingDirectory "K:\docker_class"

    Start-Process -FilePath "powershell" -WorkingDirectory "K:\docker_class" -ArgumentList 'E:\WIP-IT\USE-Scripting\vagrant_up.ps1' 

    And, that works.

    But, if you know how to do what I need in a single Start-Process command, let me know.

    Thanks again.


    Friday, January 10, 2020 4:12 PM
  • To understand how to manage a PowerShell session just read the help:

    powershell /?


    \_(ツ)_/

    Friday, January 10, 2020 4:42 PM
  • I'll look into it. Thanks.
    Friday, January 10, 2020 4:57 PM