简体   繁体   中英

How to run a powershell script in runonce-Key?

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?

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' " 

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM