
Get-Icon -Path 'C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe' Pretty simply, right? All you have to do is copy and paste this and you are ready to go!Īs I mentioned earlier, I do have a function called Get-Icon that will do all of this for you…well with the exception of actually saving the file, but you can use the knowledge here to do that with no effort at all.Ī few examples of it in action are below. The only thing left to do is convert those bytes into the encoded string by using ToBase64String() which resides in the type that is available to use. What is cool is that we already did most of the work previously with the MemoryStream object that we created. Again, there isn’t a ToBase64() method, but that is ok. The last part I wanted to show is converting this to a Base64 encoded string that you could use to supply an icon to a WinForm or WPF control as an image. I also call Flush() and Dispose() at the end to clean up after myself. Set-Content -Path 'C:\Users\boepr\Desktop\SomeFile.bmp' -Encoding Byte From there you could just pipe that information into Set-Content and use the –Encoding Byte to write those bites to a file that would then look just like what we did with saving a bitmap. What happens after I call ToArray() is that you are then bombarded with a lot of byte information in the console. $MemoryStream = New-Object System.IO.MemoryStream This becomes a job for System.IO.MemoryStream which I can use to store the byte array and then present it as output. $Icon.ToBitmap().Save('C:\users\boepr\Desktop\SomeFile.bmp')Īnd we can see the new file with the actual icon when we open it up.Ĭonverting to bytes isn’t as simple as just calling a method called ToBytes(). I’m taking the simple path tonight to save the file. Save() takes a number of parameters with the simplest being just the file path. I now have my object which I can just leave be or take it a step further by converting it to a bitmap using the ToBitmap() method.įortunately, converting this to Bitmap is as simple as calling the method: ToBitmap() and it does all of the work for you.įrom there we can actually save the file as a bitmap using the Save() method in the bitmap object.

$Path = 'C:\Users\boepr\Desktop\Get-Icon.ps1' We can then use that method to pull the icon as shown below: The method in question is ExtractAssociatedIcon which comes with the class and accepts a parameter of a string path name. To get started, I need to load up an assembly to take advantage of a method that will help to extract the icon image from the file. The function that I created, called Get-Icon should not be confused with the awesome work that fellow MVP Chrissy Lemaire did with Export-Icon as hers can dig into a DLL to pull out many icons while mine is focused on the grabbing the existing icon for a particular file type.įor my examples, I am going to use the icon for my own Get-Icon.ps1 file which has the familiar PowerShell image that we all know and love. Or maybe you just want to gather all of the icons and throw them into their own folder for whatever reason.
Unc folder icon png windows#
Sometimes you want to use an icon of a particular file for another purpose such as working with a custom UI in either WinForms or Windows Presentation Foundation (WPF) where you can supply the Base64 data to automatically load the image into the form.
