I'm putting together a powershell (running on Windows 7) which performs some configuration tasks post-image.

It uses windows forms to get some user input and then should run various tasks depending on their choice.

This runs before the computer is on the domain, so login scripts are no good.

I've tried adding powershell c:\scripts\myscript.ps1 to hklm\software\microsoft\windows\currentversion\run but it doesn't run.

I've also added it as a scheduled startup (and logon) task, with "highest level of privileges" and running as a machine admin.

When it runs as a startup task, it runs the first bit of the script (it creates a log file, so I can see this) but no windows form appears. Instead it behaves as if the user has clicked cancel on the form. The scheduled task reports with error: 0x41301. A bit of googling shows this means the script is still running?

Has anyone got any suggestions on how best to acheive this?

Thanks,

Ben

link|improve this question

77% accept rate
feedback

4 Answers

Use

powershell -File c:\scripts\myscript.ps1

instead.

link|improve this answer
Note: the -File argument is not present in v1 of PowerShell. It was introduced in v2. – Goyuix Jun 27 '10 at 16:18
@Goyuix: They asked about Windows 7 where PowerShell 2 is installed by default. – Joey Jun 27 '10 at 21:32
feedback

PowerShell.exe doesn't take a script name as a command line argument. You need to tell it to source that script to run it:

PowerShell.exe "& 'c:\path with\spaces\script.ps1'"
link|improve this answer
feedback

Like Goyuix said you must use the & - Call operator to pass it a path with spaces.

powershell.exe -Command "& 'c:\path with space\script1.ps1' arg1" 

Powershell.exe - Passing Command Arguments with Spaces

link|improve this answer
feedback

You should do it has a start up script in GPO.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.