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.

Start, Stop & Re-Start Windows Service from PowerShell

Start

start-service FIMService

 

Stop

stop-service FIMService

 

Restart

restart-service FIMService

FIMService is the Forefront Identity Manager Service. You can replace this name with the name of the service upon which you’d like to perform any of the above action.

Here’s a zip file which contains the following ps1 script and a csv file.The script will loop through the associated csv file and as per the config, will start, stop and re-start the given service(s). It will also create a Logs folder in the directory from where the script will be executed. All the logs pertaining to the current operation will be stored in this Logs directory.

 



Function LogMessage($strMsg, $lgFlNm)
{
	$strMsg | out-file -Filepath $lgFlNm -append
}

$filePath = [System.IO.Path]::Combine($PSScriptRoot, “config.csv”)
$logFilePath = [System.IO.Path]::Combine($PSScriptRoot, “Logs”)

$logFileName = [System.IO.Path]::Combine($logFilePath, ("ServiceLog_" + [string](get-date -format "yyyyMMdd_hhmmtt") + ".txt"))
$configFile = Import-Csv $filePath

foreach ($servc in $configFile){
	$servc | ForEach-Object {

		try{
			if([String]::IsNullOrWhiteSpace($_.Start) -ne $true){
				LogMessage (“************************************************************”) $logFileName
				LogMessage (“Start:: “ + $_.Start) $logFileName
				LogMessage (start-service $_.Start -passthru *>&1) $logFileName
			}
		}
		catch{
			LogMessage (“Error:: “ + $_.Exception.Message) $logFileName
		}

		try{
			if([String]::IsNullOrWhiteSpace($_.Stop) -ne $true){
				LogMessage (“************************************************************”) $logFileName
				LogMessage (“Stop:: “ + $_.Stop) $logFileName
				LogMessage (stop-service $_.Stop -force -passthru *>&1) $logFileName
			}
		}
		catch{
			LogMessage (“Error:: “ + $_.Exception.Message) $logFileName
		}

		try{
			if([String]::IsNullOrWhiteSpace($_.Restart) -ne $true){
				LogMessage (“************************************************************”) $logFileName
				LogMessage (“Restart:: “ + $_.Restart) $logFileName
				LogMessage (restart-service $_.Restart -PassThru *>&1) $logFileName
			}
		}
		catch{
			LogMessage (“Error:: “ + $_.Exception.Message) $logFileName
		}
	}
}