Rate This
  • Comments 4

The other day, a friend over in Microsoft Research wanted to figure out how to get out the width and height of an image in PowerShell.  There are many ways that you can approach this particular problem.  I knew three right off of the top of my head, but none of them had a really optimal experience.

  • I could use Windows Desktop Search, but then I’d have to wade through shell properties and it would not work when the images haven’t been indexed.
  • I could use Windows Media Player (New-Object –ComObject WMPlayer.OCX), but I’d also have to wade through a lot of metadata information that’s indexed by #, so this wouldn’t be ideal.
  • I could use the Shell Properties, but this too would involve a lot of translating property numbers into property names.

I knew there had to be a better way.  Some object had to be able to give me back image width and height as a nice simple property.

I normally use two functions to find my way around unfamiliar problems (before I start using search engines).  One I’ve shared out a few times is called Get-Type.  Get-Type will search anything loaded in .NET for a potential solution.  The problem with Get-Type is that not every type .NET has is always loaded, and loading up these types is fairly expensive.  COM objects, however, are listed in the registry.  Since we can crawl the registry in PowerShell, it’s simple enough to go ahead and check through all of the ProgIDs.

Here's my Get-ProgID function.  The second example is how I found some cool image manipulation objects I can use from PowerShell.

function Get-ProgID {                       
    #   Gets all of the ProgIDs registered on a system            
    #   Gets all ProgIDs registered on the system.  The ProgIDs returned can be used with New-Object -comObject            
    #   Get-ProgID            
    #   Get-ProgID | Where-Object { $_.ProgID -like "*Image*" }             
    $paths = @("REGISTRY::HKEY_CLASSES_ROOT\CLSID")            
    if ($env:Processor_Architecture -eq "amd64") {            
    Get-ChildItem $paths -include VersionIndependentPROGID -recurse |            
    Select-Object @{            
    }, @{            
            if ($env:Processor_Architecture -eq "amd64") {            
            } else {            

Hope this Helps,

James Brundage [MSFT]

Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post
  • Here is something I've used in the past typically to find the ProgID of a specific executable I'm trying to invoke. However it can be used to find any and all COM components which have ProgID's which will be the vast majority

    This lets the WMI class Win32_ClassicCOMClassSetting do the heavy work.

    Get-WMIObject Win32_ClassicCOMClassSetting | ? {$_.ProgId -like "*"} | select Description, ProgID, InprocServer32, LocalServer32

  • Or even easier and without all the unwrapping and marshaling overhead,


    function Get-ImageSize {

       param (

           [string[]] $Path


       begin {

           $null = [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


       process {

           $Path | foreach-object {

               $bmp = new-object System.Drawing.Bitmap -arg $_





  • Yeah, Bitmap, or just Image ...

    [system.drawing.image]::FromFile( $path ).Size

  • I gotta say though, I tried Get-ProgID, and it's a pretty slick way of expanding the list of objects I have available to me, even if it wasn't really necessary for this particular problem. :)  I added it to my tool chest.

Page 1 of 1 (4 items)