1

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

4 Answers 4

3

Use

powershell -File c:\scripts\myscript.ps1

instead.

3
  • Note: the -File argument is not present in v1 of PowerShell. It was introduced in v2.
    – Goyuix
    Jun 27, 2010 at 16:18
  • @Goyuix: They asked about Windows 7 where PowerShell 2 is installed by default.
    – Joey
    Jun 27, 2010 at 21:32
  • Works fine with anything but hklm\software\microsoft\windows\currentversion\run
    – nickl-
    Apr 8, 2013 at 5:20
2

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

1
  • 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, 2013 at 5:16
0

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

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

You must log in to answer this question.

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