title: Powershell change taskbar icon of running console application
body: <p>I'm using code from <a href="http://gallery.technet.microsoft.com/scriptcenter/9d476461-899f-4c98-9d63-03b99596c2c3" rel="nofollow">this post over at Microsoft's TechNet</a> to change the icon of my running PowerShell application. This works great for the icon that is displayed in the Powershell window itself, but it doesn't change the Taskbar's icon. I changed the function a bit an hoped that it would also change the icon displayed in the Taskbar.</p>
  
  <pre><code># Set the icon of the current console window to the specified icon.
  #
  # AUTHOR:    Aaron Lerch &lt;http://www.aaronlerch.com/blog&gt;
  # COPYRIGHT: © 2009 Aaron Lerch
  # LINK:      http://gallery.technet.microsoft.com/scriptcenter/9d476461-899f-4c98-9d63-03b99596c2c3
  #
  # PARAM:
  #   -IconFile
  #     Absolute path to the icon file.
  # RETURN:
  #   $null
  function Set-ConsoleIcon {
    Param(
      [parameter(Mandatory = $true)] [string] $IconFile
    )
  
    [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
  
    # Verify the file exists
    if ([System.IO.File]::Exists($iconFile) -eq $true) {
      $ch = Invoke-Win32 'kernel32' ([IntPtr]) 'GetConsoleWindow'
      $i = 0;
      $size = 16;
      while ($i -ne 4) {
        $ico = New-Object System.Drawing.Icon($iconFile, $size, $size)
        if ($ico -ne $null) {
          Send-Message $ch 0x80 $i $ico.Handle | Out-Null
        }
        if ($i -eq 4) {
          break
        }
        $i += 1
        $size += 16
      }
    }
    else {
      Write-Host 'Icon file not found' -ForegroundColor 'Red'
    }
  }
  </code></pre>
  
  <p>I'm providing the icon in the sizes <code>16</code>  (<code>wParam 1</code>), <code>32</code> (<code>wParam 2</code>), <code>48</code> (<code>wParam 3</code>), and <code>64</code> (<code>wParam 4</code>).</p>
  
  <p>I also tried to change the Icon from my launching C# application (based on <a href="https://stackoverflow.com/questions/9199523">this Stackoverflow discussion</a>) but that didn't work at all.</p>
  
  <p>If you'd like to see the complete code have a look at the following:</p>
  
  <ul>
  <li><a href="https://github.com/MovLib/www/blob/master/conf/vagrant/Visual%20Studio%20Project/MovLib%20Vagrant%20Bootstrap/Program.cs" rel="nofollow">the C# class that starts the Powershell process</a></li>
  <li><a href="https://github.com/MovLib/www/blob/master/conf/vagrant/bootstrap.ps1" rel="nofollow">the Powershell PS1 script</a></li>
  </ul>