Friday, March 20, 2009

Get-ProgID

 

Get-ProgID

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 {                       
#.Synopsis
# Gets all of the ProgIDs registered on a system
#.Description
# Gets all ProgIDs registered on the system. The ProgIDs returned can be used with New-Object -comObject
#.Example
# Get-ProgID
#.Example
# Get-ProgID | Where-Object { $_.ProgID -like "*Image*" }
param()
$paths = @("REGISTRY::HKEY_CLASSES_ROOT\CLSID")
if ($env:Processor_Architecture -eq "amd64") {
$paths+="REGISTRY::HKEY_CLASSES_ROOT\Wow6432Node\CLSID"
}
Get-ChildItem $paths -include VersionIndependentPROGID -recurse |
Select-Object @{
Name='ProgID'
Expression={$_.GetValue("")}
}, @{
Name='32Bit'
Expression={
if ($env:Processor_Architecture -eq "amd64") {
$_.PSPath.Contains("Wow6432Node")
} else {
$true
}
}
}
}


Hope this Helps,



James Brundage [MSFT]



Published Friday, March 20, 2009 11:24 PM by PowerShellTeam




Windows PowerShell Blog : Get-ProgID

No comments:

Blog Archive