Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

Use

powershell -File c:\scripts\myscript.ps1

instead.

share|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
    
Works fine with anything but hklm\software\microsoft\windows\currentversion\run –  nickl- Apr 8 '13 at 5:20

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

share|improve this answer
    
This proves to be the true answer as none of the others would succeed when configured as a logon Run script at hklm\software\microsoft\windows\currentversion\run. You can pass environment variables with the PS flavour of $env ex. powershell -Command "& $env:USERPROFILE'\scriptme.ps1'" –  nickl- Apr 8 '13 at 5:16

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'"
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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