PowerShell Team

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*" } 
    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]


Discussion is closed.

Feedback usabilla icon