How to run a powershell script in runonce-Key?
Asked Answered
Y

4

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?

Yeti answered 20/6, 2012 at 12:57 Comment(0)
D
8

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' " 
Downtime answered 20/6, 2012 at 13:3 Comment(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?Yeti
And what if you want to change the working directory of the script?Toolmaker
R
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.

Reggie answered 20/6, 2012 at 13:41 Comment(0)
R
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"
Rake answered 20/6, 2012 at 15:27 Comment(0)
B
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")
Bronson answered 26/8, 2023 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.