title: Use PowerShell to create a shortcut to launch a PowerShell script
body: <p>I have a need to create a shortcut on a Windows 2019 Server for the Eclipse application so that it runs a PowerShell script instead of opening Eclipse. The script runs a few commands first and then opens the Eclipse application. When I edit an existing shortcut for Eclipse, I can modify the target to be:</p>
  <pre><code>&quot;powershell.exe -ExecutionPolicy bypass C:\temp\eclipse-fix.ps1 -WindowStyle Hidden&quot;
  </code></pre>
  <p>This works fine when completed manually. However, when I try to do the same with a PowerShell script, to automate the process, I get an error.</p>
  <p>Contents of the script:</p>
  <pre><code>$WshShell = New-Object -comObject WScript.Shell
  $Shortcut = $WshShell.CreateShortcut(&quot;C:\Users\Administrator\Desktop\Eclipse.lnk&quot;)
  $Shortcut.TargetPath = &quot;C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy bypass C:\cfn\temp\eclipse-fix.ps1 -WindowStyle Hidden&quot;
  $Shortcut.IconLocation = &quot;%SystemDrive%\eclipse\eclipse.exe&quot;
  $Shortcut.Save()
  </code></pre>
  <p>Error Returned:</p>
  <pre><code>PS C:\Users\Administrator&gt; $WshShell = New-Object -comObject WScript.Shell
  PS C:\Users\Administrator&gt; $Shortcut = $WshShell.CreateShortcut(&quot;C:\Users\Administrator\Desktop\Eclipse.lnk&quot;)
  PS C:\Users\Administrator&gt; $Shortcut.TargetPath = &quot;powershell.exe -File C:\cfn\temp\eclipse-fix.ps1&quot;
  Value does not fall within the expected range.
  At line:1 char:1
  + $Shortcut.TargetPath = &quot;powershell.exe -File C:\cfn\temp\eclipse-fix. ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException
  
  PS C:\Users\Administrator&gt; $Shortcut.IconLocation=&quot;%SystemDrive%\eclipse\eclipse.exe, 0&quot;
  PS C:\Users\Administrator&gt; $Shortcut.Save()
  </code></pre>
  <p>Result:</p>
  <p>The icon is created but has no value for target.</p>
  <p>Does anyone know what I am missing to get this to work? Is there another option to use that I may not be aware of?</p>
  <p>Thanks.</p>