.NET Scripting using PowerShell

Late Binding for .Net

Just as ‘Late Binding’ for COM objects. We can use the same ‘Late Binding’ approach for .Net Scripting, like PowerShell.

Since PowerShell is built on top of .Net we can use .NET Framework’s Reflection API’s.

.NET Framework’s Reflection API’s allows you to fetch type (assembly) information at runtime programmatically. We can also achieve late binding by using .NET Reflection.

At runtime, the Reflection mechanism uses the PE file to read information about the assembly. Reflection enables you to use code that is not available at compile time.

It can be used effectively to find all types in an assembly and/or dynamically invoke methods in an assembly. This includes information about the type, properties, methods, and events of an object.

The System.Reflection namespace and the System.Type class plays a very important role in .NET Reflection.

You can read more interesting stuff in this article.


Now move back to PowerShell

Add-Type versus Reflection. In the early versions of PowerShell there was no option. You had to use the .NET Reflection method to load assemblies.

Both allow you to hook into the power of the .NET Framework as well as any Assemblies DLL you build.

As of Powershell V2.0 the This allows you to tap into the power of the .NET framework as well as any DLLs/Assemblies you build. was introduced.

Let’s see some differences :

In PowerShell Version 1 that cmdlet is not available so you need to use the one of the Load* static methods found in System.Reflection.Assembly.

[Reflection.Assembly]::Load Loads an assembly
[Reflection.Assembly]::LoadFile Loads the contents of an assembly file on the specified path
[Reflection.Assembly]::LoadFrom Loads an assembly given its file name or path
[Reflection.Assembly]::LoadWithPartialName Loads an assembly from the application directory or from the global assembly cache using a partial name

Reflection vs Add-Type Example here :

$null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$date = [Microsoft.VisualBasic.Interaction]::InputBox('What is your birthday?')
$days = (New-TimeSpan -Start $date).Days
"You are $days days old."

# Using the Add-Type cmdlet:
Add-Type -AssemblyName Microsoft.VisualBasic<br>$date = [Microsoft.VisualBasic.Interaction]::InputBox('What is your birthday?')

$days = (New-TimeSpan -Start $date).Days
"You are $days days old."

Both will have the same result. More interesting to read over here  :


Last but not least you can also call a .Net Class directly.

Example calling a .Net Class directly  :

# .NET Version
if ([System.IO.Directory]::GetParent("C:\windows") -ne $null)
# do something interesting
$NETDirInfo = New-Object System.IO.directoryInfo -ArgumentList "C:\windows"

echo "NETDirInfo"

# PS Version
if ((test-path c:\windows) -eq $true)
	# do something interesting
	$PSDirInfo = Get-ItemProperty c:\windows
	echo "PSDirInfo"


As you can see both have the same result 🙂

More in-depth reading here :


I hope this can help clear some mist ?

So now it is time to find out which Assemblies, Classes, Method, Properties etc. are at your disposal.

Example to get Assembly information

This will list a all of the Assemblies Class information

[appdomain]::currentdomain.GetAssemblies() | % {$_.gettypes()} | sort basetype



Different Approach using New-Object

Last but not least you can access the Assembly Methods & Properties using the New-Object –> Get-Member #-static for static members only

New-Object -com scripting.filesystemobject | Get-Member


Not handy so we need some extra Tools read here

Enjoy !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: