5

I have a call to powershell.exe that looks like this, and which works from my commandline:

powershell.exe -noexit -command " & 'C:\Test\test.ps1' "

However, when I enter that entry exactly that way in the runonce-key of my current user, nothing happens.

What is the correct way to call powershell.exe passing parameters from runonce or run?

CB.
  • 58,865
  • 9
  • 159
  • 159
Erik
  • 2,316
  • 9
  • 36
  • 58

4 Answers4

7

Try with full path:

c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit "C:\Test\test.ps1" 

or

c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command " & 'C:\Test\test.ps1' " 
CB.
  • 58,865
  • 9
  • 159
  • 159
  • 2
    Thanks, it was an issue with the path, it works in commandshell because I guess the powershell-path is in my environment settings when already logged on, but not yet when the runonce-key is executed. However, I made the path a bit more universal: %WINDIR%\system32\WindowsPowerShell\v1.0\powershell.exe, and use ExpandEnvironmentStrings before adding that path from another powershell-script to the registry. Will this also work for Win7 x64? – Erik Jun 21 '12 at 05:27
  • 1
    And what if you want to change the working directory of the script? – Mathias Lykkegaard Lorenzen Apr 02 '16 at 08:13
2

A slightly different approach would be to put all your call to powershell.exe with all arguments / parameters into a simple *.bat file, and call that *.bat file from your RunOnce Key. This way you don't need to play around with params, which can mess-up sometimes. Your PowerShell script could even delete that *.bat file, since it is required only once.

trejder
  • 17,148
  • 27
  • 124
  • 216
1

Use the File argument instead of command. You can also use some of the other parameters to clean things up (noprofile, sta, and/or WindowStyle)

This is an example of what I use.

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -sta -WindowStyle Hidden -File "C:\Test\Test.ps1"
Schlauge
  • 483
  • 1
  • 4
  • 7
0

You can wrap it in a cmd. That way it has access to the normal PATH and can find powershell.

e.g.

cmd /c (powershell -ExecutionPolicy Bypass -file "C:\Test\test.ps1")
GrahamS
  • 9,980
  • 9
  • 49
  • 63