Tuesday, April 05, 2011

Goodbye [reflection.Assembly]::LoadFrom, Hello Add-Type!

One of the things I love about PowerShell is that almost everyday I learn something new that makes me better at what I do.  Not just better at PowerShell, but better at getting my stuff done (testing, deploying, automating, integrating, etc).  It’s like that excitement of finding $5 on the sidewalk.  You’re both happy to be a little richer but also harbour that sneaky feeling that you’ve stolen something, in this case you’ve stolen time, and maybe money too.

Anyhow, my $5 bill today is the discovery of Add-Type.

For too long I’ve been using [reflection.Assembly]::LoadFrom to load assemblies, which is analogous to adding references to a C# project.  It is a bit tedious and I don’t feel like I’ve ever mastered it, but now have found Get-Type to be way more user friendly and dependable.

001
002
003
004
005

### Before
[reflection.Assembly]::LoadFrom('C:\Windows\….<path shortened>\System.Security.dll'

### After
Add-Type -AssemblyName System.Security

No comments: