When to use .NET namespaces within PowerShell

There have been some debate regarding when to use .net namespaces within PowerShell. Some people will call .net classes all the time just because they are used to write in C#, C++, etc. Just keep in mind the code you write most likely will be used by other people, so be conscious about your audience. But as a good rule of thumb I do this.

  1. Try to write with the PowerShell cmdlets available
  2. If there are not cmdlets for what I want to do, then  I use the CIM classes
  3. Finally use .NET

However, every rule has an exception and in the next couple of years is going to become the rule. When managing large scale systems or dealing with a lot of data PowerShell may not be an efficient tool to handle those tasks.

Let me give you a  quick example

If i need to write  all the numbers between  1 and 10,000 to a file I would do something like this


Using cmdlets this process will take around 96 seconds


Now let’s see how long the same process will take using the .net namespace system.io



As you can see with .net system.io namespace it only took 0.31 seconds. There is a 95 seconds difference between PowerShell and .Net.

I am not saying go start writing everything in .Net but keep in mind when dealing with scale you may need to start looking into it




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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s