By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

was one of my biggest obstacles when learning PowerShell since I’m not a developer and most of the programming concepts were somewhat foreign to me.

When I first started working with PowerShell, I didn’t know the differences among classes, interfaces, constructs, or members, nor did I truly understand the concept of objects (from a programmer’s point of view). But I do think these concepts are critical to learning and using .NET with Windows PowerShell. I’ve also found that knowing a little C# (or at least being able to read it) is incredibly helpful.

Therefore, I will attempt to explain these concepts briefly through examples and provide some code to help you through the process. I’ll also try (with my limited ability) to provide some basic guidance in C# PowerShell translation.

Let’s start with definitions. Again, if you’re a developer please feel free to comment on anything I may be off about. These definitions are my opinion alone on what these concepts entail.

.NET: It is a framework of black-boxed code that accepts specific input and returns either a value or object. Basically, Microsoft did all the coding for you -- you just have to call on it correctly. MSDN is an invaluable resource for the .NET Framework.

Class: Almost everything I reference in .NET is a class. I like to think of a class as a template for what an object should look like and what properties and methods it should have. For example, a Microsoft.Win32.RegistryKey object should have a Name Property and a GetValue method.

Members: Every class has members, which are properties and methods combined. It’s a good place to look if you just want to see an overview of what a class has to offer.

Properties:Properties are one of the two types of members a class can have. Simply put, they can be thought of as attributes of an object. For example, Microsoft.Win32.RegistryKey class has properties like Name, SubKeyCount and Value Count, so every Microsoft.Win32.RegistryKey object can have those properties.

Methods:Methods are one of the two types of members a class can have. Like properties, methods are also defined by the class, but instead of an attribute, they are more like a function of the class. Microsoft.Win32.RegistryKey class has CreateSubKey, DeleteSubkey, SetValue and so on. As with properties, every Microsoft.Win32.RegistryKey object can have those methods.

Constructor: A constructor is a method of a class that collects the information required to create an instance of that class. I like to define constructs as the information or objects required to make an object useful. Let’s use System.Data.SqlClient.SqlCommand as an example. It has four different ways you can create an object from the class. Each one creates an object with slightly different data.

Static fields: These are just like properties, but are available without having to create an instance of the object. In PowerShell, accessing static members is really simple. All you have to do is use [.NET Class]::StaticField.

In the following example, the .NET class System.Math will return the value of PI:

[System.Math]::PI.

Static methods: These are just like methods, but are available without having to create an instance of the object. Similar to how you access static fields, all you have to do is use [.NET Class]::StaticMethods(“arguments”).

In the following example, Microsoft.Win32.RegistryKey will return a Microsoft.Win32.RegistryKey object for a remote machine called MyServer.

Now that we’ve covered the basics of .NET Framework, let’s go through some of the PowerShell specifics like creating classes and discovering what properties and methods are available for those classes.

New-Object -- Creates an instance of a Microsoft .NET Framework or COM object.

In the following example I am creating an instance of System.DirectoryServices.ActiveDirectory.DirectoryContext:

ABOUT THE AUTHORBrandon Shell has been in the IT industry since 1994. He started out as a PC tech and general fix-it guy for numerous companies. In 2007, he joined the PowerShell MVP ranks, and Shell has spent the past several years building his PowerShell knowledge and helping others build theirs.

0 comments

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy