Schedule a PowerShell Script to run on Windows Startup

By default, ps1 files open with the default text-editor. So mere putting it in the startup folder will only open the file at the startup instead of them actually getting executed. In order to execute the same, we need to trigger a startup job which will fire the PowerShell in the non-interactive mode with the file path as argument. Following are the steps:* Open the Windows PowerShell in admin mode.

* Create a new job trigger. Here, I am introducing a delay of 10 mins.

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:10:00

* Schedule the job

Register-ScheduledJob -Trigger $trigger -FilePath C:\Some-Path\StartStopServices.ps1 -Name StartStopServices

View the scheduled job

* Open Task Scheduler and navigate to Microsoft => Windows => PowerShell. You can find here, the newly scheduled job.

* Open its properties. Since, the script will check the state of some Windows Services on startup. I’ll select the ‘Run with highest privileges‘ option.

* Go to the Action tab and select Edit Action. Here, you can see that it’s the PowerShell, which will be fired at the startup, and it contains the script path as argument along with some other properties.

* Finally, at the time of saving, admin password box will be prompted. This is because, I have marked the job to run with the highest privileges.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s