Making Batch to run Windows PowerShell, then have it run some text

43,738

As you have not provided your complete bat-file I guess that it is

Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'"
PowerShell -NoExit -Command "Write-Host 'node sp'"

This is the wrong approach. You are first starting a Powershell which changes to the given directory and then stays open. If you exit this powershell the bat file will start the second PowerShell.

You need to run one PowerShell and let it execute both commands. One way is to

Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'; Write-Host 'node sp'"

Another way is to write a PowerShell script file like MyScript.ps1 with this content:

cd 'c:\Dev\ProductDev'
Write-Host 'node sp'

and start PowerShell not giving it the command to execute but the script to execute (see also this and this StackOverflow question). You could either run this command directly or put it in a bat file, or even use it as the command to execute in a lnk file:

PowerShell.exe -noexit -ExecutionPolicy Bypass -File "MyScript.ps1"

A problem with PowerShell is that per default it does not run scripts that are not (cryptographically) signed. The easy way to solve that is the -ExecutionPolicy Bypass parameter.

Share:
43,738

Related videos on Youtube

Austin
Author by

Austin

Updated on September 18, 2022

Comments

  • Austin
    Austin over 1 year

    I am newer to making batch files. I am trying to run a command that will open windows PowerShell in a specific directory, and then once there to run a command.

    So far I have the following. Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'" This seems to work in opening PowerShell to the right location, but I cannot get it to then run the command node sp.

    I have tried the following with no success PowerShell -NoExit -Command "Write-Host 'node sp'"

    Also, is it possible to have it open Windows PowerShell in the blue display screen, rather than in the CMD (black/white) window?

    • sean christe
      sean christe about 9 years
      I'm not sure why you wouldn't just write the script in Powershell?
    • Austin
      Austin about 9 years
      @EBGreen Explain? Sorry, I am new to writing scripts in general.
    • Austin
      Austin about 9 years
      All I am trying to do is make a shortcut or two to run some things for me, but want to see/view them in PowerShell's screen rather than CMD.
    • sean christe
      sean christe about 9 years
      If I understand, right now you are opening a file in an editor of some sort (probably notepad) and you are typing batch commands probably one per line then saving the file with a .bat extension. Thus making a batch file. Powershell is a scripting language as well (and more versatile than batch). You would do something very similar. Open a file in an editor, type in powershell commands then save it as a .ps1 file. There are guides all over the internet for getting started in Powershell.
    • Austin
      Austin about 9 years
      @EBGreen ohhh did not know that. Thank you!
  • Austin
    Austin about 9 years
    I shall give this a shot! For the second part, to run the powershell script, is that a bat file then?
  • Werner Henze
    Werner Henze about 9 years
    I adjusted my answer, hopefully it's clearer now. The third code block in my answer is the content of the new script file MyScript.ps1. The fourth (and last) code block is a line that you can directly enter in cmd.exe or that you can put into a bat file or that you can use as the command line in a lnk file.
  • Austin
    Austin about 9 years
    If it matters, the node sp command is basically a serve to run an application, so its a constantly running/watching command until I exit it.
  • Werner Henze
    Werner Henze about 9 years
    To debug you can run cmd.exe and run your test.bat there, so you see the output. The reason the powershell closes is that I forgot the -noexit. I updated my answer so it is included.
  • sean christe
    sean christe about 9 years
    Why run cmd at all? Why not do everything in a powershell prompt?
  • Werner Henze
    Werner Henze about 9 years
    @EBGreen Doesn't matter, both ways are possible. Whatever Austin prefers. I just meant to first start a shell so the window stays open after the script exits.