title: powershell script does not run properly from a batch file
body: <p>I have a functioning PowerShell script which registers a timer object and sends a key every couple of minutes.</p>
  
  <pre><code>$wshell = New-Object -ComObject Wscript.Shell    
  
  ...
  
  function fctName {
      param([ScriptBlock] $action)
  
      ...
  
      Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $action
  }
  
  fctName {
  
      ...
  
      $wshell.sendkeys("{ESC}")
  }
  </code></pre>
  
  <p>Within the Windows PowerShell ISE the script works just fine, without any errors.
  But, I would love to run it from a batch file, in which I wrote only this line:</p>
  
  <pre><code>Powershell.exe -noexit C:\...\ScriptName.ps1
  </code></pre>
  
  <p>No errors are thrown but it does not do what I want, which is to prevent Windows going idle.</p>
  
  <p>Because I '-noexit' the cmd window, I can easily stop the event with</p>
  
  <pre><code>Unregister-Event -SourceIdentifier Timer.Elapsed
  </code></pre>
  
  <p>It seems to be working because only typing it twice throws an error.</p>
  
  <ul>
  <li>Could it be that my batch file needs different code?
  Maybe "echo" etc. I'm not very familiar with it</li>
  <li>Could it be that I have to import a module in Powershell?</li>
  </ul>