troubleshooting Question

Assistance with PowerShell Scripting - Windows Scheduled Tasks

Avatar of Brad Sims, CCNA
Brad Sims, CCNAFlag for United States of America asked on
PowershellWindows 10Windows OS
5 Comments1 Solution130 ViewsLast Modified:
I am trying to get PowerShell to run a Scheduled Task if it exists, or create a scheduled task if it doesn't.  I have verified the locations of the files and that the computer names are online and reachable (I have one here at my desk as a test).  The script looks like it runs, and I don't get any errors, but the task isn't created on the remote machine like it should be.  

Did I miss something simple?  Or is my code just wrong.  I am still trying to learn PowerShell, so either scenario is very possible.

$computers = Get-Content -Path C:\Users\****\Desktop\Computers.txt

$taskname = "McAfee Agent Update"
$taskdescription = "Update McAfee Agent Using Service Account"
$action = New-ScheduledTaskAction -Execute '\\share\McAfee.bat' `
  -Argument '-NoProfile -WindowStyle Hidden -command "& Restart-Service -displayname \"McUpdates\""'

foreach ($computer in $computers) {

$taskexists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskname }

if($taskexists) {

Start-ScheduledTask -TaskName $taskname

} else {

$ts = New-TimeSpan -Days 0 -Hours 0 -Minutes 1
$trigger =  New-ScheduledTaskTrigger -Once -At ((Get-Date) + $ts) 
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 2) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)

Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Settings $settings -User "System"



Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros