4

I have a powershell script that I want to run on my PC every time I log on. I've added it as a REG_SZ in my current user "run" key, but it never executes.

I've altered the execution policy to "unrestricted", but still no luck.

The contents of the registry value are like this:

powershell.exe c:\path\myscript.ps1

That does work from the command prompt. Any idea why it won't run from the registry?

Thanks.

2
  • Yes, I mean the startup folder, I'm sorry for that, my system is in german. Why do you think this won't work? Editing the registry is a bit more complicated because not everything is what it looks like.
    – Michael K
    Commented May 17, 2011 at 12:11
  • No probs about the language. As I say, startup may work, but I'd really like to know why the registry doesn't. Commented May 17, 2011 at 12:41

4 Answers 4

3

I usually run PowerShell scripts this way:

powershell.exe -file c:\path\myscript.ps1

For more information, run this from the command line:

powershell.exe -help
2
  • I'll have a look at that but my command above does work from the command prompt. Thanks Commented May 17, 2011 at 14:57
  • This does work if you change the execution policy to remote signed. Still have no idea why you need to change that or use the "-file" switch. Thanks for your help. Commented Aug 15, 2011 at 20:02
1

Today I read about a powershell script that continues after rebooting the computer. To do this, the scripter automatically writes to his run-key. If you take a look at this blogpost, you'll see that he just writes the scriptname plus its params to the registry key.

I wasn't able to test it yet, but if it's worked for him, that would mean, that you don't append "powershell.exe" in front of the scriptname.

3
  • Thanks, that does run it - but only by opening it in Notepad - doesn't actually run the script. At least I now know it is being accessed for sure :) Commented Jul 20, 2011 at 19:39
  • Then you might have a look at this technet-question. There is said that the easiest way to start a script is through a batch-file. Another problem could be the restriction-policy. You could also try to start your powershell-script like in the second proposal from jrich by appending '-executionpolicy unrestricted' to the powershell argument list.
    – wullxz
    Commented Jul 22, 2011 at 7:40
  • Forgot that: I don't know if the changed executionpolicy is valid only for this powershell-session that is started from the registry (can't test it right now). You have to test it and propably reset the policy to "restricted" in your script.
    – wullxz
    Commented Jul 22, 2011 at 7:43
1

Ok, I got this solved. You need to set the execution policy to remotesigned and not "bypass" as Microsoft told me. To do that run this command in powershell:

set-executionpolicy remotesigned

Then this is the REG_SZ in my registry run key

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file c:\path\myscript.ps1

So now I have a powershell script that starts all my usual programs with a gap between them allowing me to get working on my machine immediately without the slowness every application trying to open at the same time.

Here is a little snippit of my *.PS1 file as others may find it useful:

# Chrome Browser
Start-Sleep -s 20
& "C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe"

# Outlook
Start-Sleep -s 10
& "c:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"

# Messenger
Start-Sleep -s 10
& "c:\Program Files (x86)\Windows Live\Messenger\msnmsgr.exe" /background

# Snagit
Start-Sleep -s 30
& "C:\Program Files (x86)\TechSmith\Snagit 10\Snagit32.exe" /i /h

# Evernote
Start-Sleep -s 60
& "C:\Program Files (x86)\Evernote\Evernote\EvernoteTray.exe"
0

If I remember right you can't execute a powershell script directly (as in double clicking it), it has to be executed from the command line. Maybe Windows sees executing from the registry as executing directly. What you could try (and I my be out on a limb here) is calling the script from a batch file and then calling the batch file from the registry.

2
  • You can but by default the Execution policy will not allow it. You will need to change it to allow scripts. I HIGHLY recommend you sign this script.
    – uSlackr
    Commented May 17, 2011 at 14:38
  • That's right you can't double click as it opens up for editing by default. So that is why starting with powershell.exe tells it how to open. This works from the command prompt so should work from the registry too. Cheers Commented May 17, 2011 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .