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?

  • 1
    I'm not sure why you wouldn't just write the script in Powershell? – EBGreen Apr 24 '15 at 14:45
  • @EBGreen Explain? Sorry, I am new to writing scripts in general. – Austin Apr 24 '15 at 14:46
  • 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. – Austin Apr 24 '15 at 14:48
  • 3
    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. – EBGreen Apr 24 '15 at 14:50
  • @EBGreen ohhh did not know that. Thank you! – Austin Apr 24 '15 at 14:51

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.

  • I shall give this a shot! For the second part, to run the powershell script, is that a bat file then? – Austin Apr 24 '15 at 15:05
  • 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. – Werner Henze Apr 24 '15 at 15:10
  • Okay cool! I did as you said! I put the fourth line into a bat with the inside being PowerShell.exe -ExecutionPolicy Bypass -File "RunProductDev.ps1", when I click it it opens CMD for a moment then closes, but my RunProductDev.ps1 never seems to run or open. They are both just sitting on my desktop right now so they are in same directory. In the ps1 I have cd 'c:\Dev\ProductDev' Write-Host 'node sp' – Austin Apr 24 '15 at 15:13
  • 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. – Austin Apr 24 '15 at 15:18
  • 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. – Werner Henze Apr 24 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.