launch powershell script from registry

4

1

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.

Kieran Walsh

Posted 2011-05-17T10:16:30.380

Reputation: 279

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 – 2011-05-17T12:11:08.397

No probs about the language. As I say, startup may work, but I'd really like to know why the registry doesn't. – Kieran Walsh – 2011-05-17T12:41:42.797

Answers

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

William Jackson

Posted 2011-05-17T10:16:30.380

Reputation: 7 646

I'll have a look at that but my command above does work from the command prompt. Thanks – Kieran Walsh – 2011-05-17T14:57:35.503

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. – Kieran Walsh – 2011-08-15T20:02:35.373

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.

wullxz

Posted 2011-05-17T10:16:30.380

Reputation: 2 400

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 :) – Kieran Walsh – 2011-07-20T19:39:30.253

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 – 2011-07-22T07:40:27.617

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 – 2011-07-22T07:43:03.730

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"

Kieran Walsh

Posted 2011-05-17T10:16:30.380

Reputation: 279

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.

CGA

Posted 2011-05-17T10:16:30.380

Reputation: 3 767

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 – 2011-05-17T14:38:58.780

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 – Kieran Walsh – 2011-05-17T14:57:04.647