title: Register-ScheduledJob "completed" but not "running" script as intended
body: <h1>Background</h1>
  
  <p>I have a powershell script that is designed to watch a folder for new files using Register-ObjectEvent.</p>
  
  <p>I can get it to run in ISE and PowerShell console just fine.  It works exactly as intended.  It watches the folder and does its job.</p>
  
  <h2>The Script That is Scheduled with Register-ScheduledJob</h2>
  
  <p>Here is the .ps1 in full for if it helps:</p>
  
  <pre><code>$HomeStatus = 'C:\status'
  $mFilter = '*.*'
  
  gci $HomeStatus -Recurse | sort LastWriteTime -desc | select -Skip 1 | Remove-Item -Force
  
  $WatchProps = @{
      Path = $HomeStatus
      Filter = $mFilter
      NotifyFilter = [IO.NotifyFilters]"FileName"
      IncludeSubdirectories = $False   
  }
  
  $Watcher = New-Object System.IO.FileSystemWatcher -Property $WatchProps
  
  $isCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier HomeStatus -Action{
      $name = $Event.SourceEventArgs.Name
      $changeType = $Event.SourceEventArgs.ChangeType
      $timeStamp = $Event.TimeGenerated
      gci $HomeStatus -Recurse | sort LastWriteTime -desc | select -Skip 1 | Remove-Item -Force
  }
  </code></pre>
  
  <h2>Creating the Register-ScheduledJob</h2>
  
  <p>I went ahead and used Register-ScheduledJob to make it run at startup:</p>
  
  <pre><code>$startTrigger = New-JobTrigger -AtStartup -RandomDelay 00:01:30 #create trigger for 90 seconds after startup
  
  Register-ScheduledJob -Trigger $startTrigger -FilePath C:\Users\x\Powershell\HomeStatus.ps1 -Name CamHomeStatus
  </code></pre>
  
  <p>That creates the job in task scheduler just fine.  I went ahead and modified the permissions in task scheduler so it runs as SYSTEM with the highest privileges:</p>
  
  <p><a href="https://i.stack.imgur.com/RyR6X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RyR6X.png" alt="enter image description here"></a></p>
  
  <h1>The Problem</h1>
  
  <p>The issue is that it seems to execute the scheduled job just fine, but the script isn't running/working in the background.</p>
  
  <p>I have tested it - first by rebooting the machine and then by running the task on demand.  From the History tab in task scheduler, it appears to be executing without a problem:</p>
  
  <p><a href="https://i.stack.imgur.com/Yv0ZJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yv0ZJ.png" alt="enter image description here"></a></p>
  
  <p>Get-Job returns the following:</p>
  
  <pre><code>PS C:\WINDOWS\system32&gt; Get-Job
  
      Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
      --     ----            -------------   -----         -----------     --------             -------                  
      1      CamHomeStatus   PSScheduledJob  Completed     True            localhost            $HomeStatus = 'C:\User...
  </code></pre>
  
  <p>But that isn't what I see if I simply run the .ps1 from ISE or PS console.  When I manually execute the script from either of those options, I see the following:</p>
  
  <pre><code>PS C:\WINDOWS\system32&gt; Get-Job
  
  Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
  --     ----            -------------   -----         -----------     --------             -------                  
  5      HomeStatus                      Running       True                                 ...                 
  </code></pre>
  
  <p>So to me it looks like the task scheduler runs the scheduled job just fine, but the once the scheduled job runs, it doesn't allow the Register-ObjectEvent job from the .ps1 to start/continue running.</p>
  
  <p>Did I invoke the script incorrectly when I registered the job, or did I miss some argument that allows the script to run after the scheduled job initializes it?</p>