How do I run a powershell with a Windows form at logon / startup?
Solution 1
Use
powershell -File c:\scripts\myscript.ps1
instead.
Solution 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
Related videos on Youtube
Ben
Updated on September 17, 2022Comments
-
Ben over 1 year
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
tohklm\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
-
JSchlather almost 14 yearsNote: the -File argument is not present in v1 of PowerShell. It was introduced in v2.
-
Joey almost 14 years@Goyuix: They asked about Windows 7 where PowerShell 2 is installed by default.
-
nickl- about 11 yearsThis 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- about 11 yearsWorks fine with anything but
hklm\software\microsoft\windows\currentversion\run