PowerShell Essentials (Part 9)

If you would like to read the other parts in this article series please go to:

When I wrapped up my previous article in this series, I mentioned that one of the most powerful ways to unlock the true potential of PowerShell is to delve into .NET. Traditionally, .NET has been the domain of developers and is something that system administrators rarely, if ever have to touch. Even so, it is worth learning a little bit about .NET because doing so will make a dramatic difference in what you can do with PowerShell.

Before I get started, let me clarify that I am not a professional developer. Don’t get me wrong. I can write code. It’s just that I don’t do development for a living and most of the code that I have written probably violates a number of established best practices. I know how to get the job done, but my code usually isn’t pretty.

The reason why I am telling you this is to reassure you that I am not about to jump straight into the proverbial abyss, and assume that you know all about .NET development. On the contrary. I am going to give you a really quick crash course in .NET and then I will show you how to use PowerShell to exploit what I have just shown you. Please believe me when I say that this is going to be a lot easier than it sounds. So with that said, let’s get started.

As you learned at the beginning of this article series, PowerShell cmdlets are made up of verb-noun combinations. Because verbs and nouns are able to be used in various combinations, you end up with groups of PowerShell cmdlets that perform similar tasks.

To show you what I mean, take a look at Figure A. In this figure, I entered the Get-Command cmdlet, followed by *-VM. This caused PowerShell to list all of the cmdlets that are based on the VM noun. These are all cmdlets that are used for working with Hyper-V virtual machines. In other words, there is a group of cmdlets that provide virtual machine functionality.

Image
Figure A: All of these cmdlets are related to virtual machines.

Conveniently, .NET is organized in a somewhat similar way. The difference is that the organizational structure is a bit more formal and the terminology is different.

If you look back at Figure A, you will see a number of PowerShell cmdlets that are each related to Hyper-V virtual machines. Even though these cmdlets are loosely related to one another, there is no formal grouping beyond the default module.

In .NET we don’t really have cmdlets per se. Instead, we have classes. A class is defined as an individual unit of functionality. If we were to continue to use virtual machines as an example, there might be a class for creating a virtual machine, a class for starting a virtual machine, and a class for renaming a virtual machine (these aren’t real classes, I am only using them as an example). In other words, each class is capable of performing one specific task.

Classes that are related to one another are grouped into a .NET structure called an assembly. You can think of an assembly as being similar to a PowerShell module. In PowerShell, a module is a collection of cmdlets. In .NET, an assembly is a collection of classes.

Another structural component that I need to quickly mention is a namespace. I will be the first to admit that namespaces can be complicated. When it comes to accessing .NET assemblies through PowerShell however, you can usually think of a namespace as a label that allows multiple classes to be used, even if those classes belong to different assemblies.

To make this a bit clearer, think about what happens when you need to use PowerShell modules. You may need to initially import the modules, but once the modules are imported you are free to use the cmdlets within those modules without addressing the module by name. The .NET namespaces work in a similar way. They allow you to use multiple assemblies without having to always reference which assembly contains which class.

One last piece of the puzzle that I need to mention is the method. Although it’s not technically precise, you can loosely think of a method as an individual .NET command. A .NET class can contain one or more methods.

So now that I have talked about the basic structure used by .NET, let’s talk about some syntax. Remember earlier when I said that .NET namespaces could apply to classes from multiple assemblies. The reason for that is that Windows includes something called the System Namespace which is a huge collection of classes. You can see the contents of the .NET system namespace here.

As you look through the list of classes, you will quickly realize that some are useful only to developers, but others could be useful in PowerShell environments. If you find a class that you want to use, the first thing that you will probably want to do is to determine which methods make up the class. To do so, you can use the following command:

[<class name>].Get-Methods() | Select-Object Name –Unique

This command displays the names of all of the methods within the class that you specify. The –Unique switch gets rid of the duplicates. To give you a more concrete example, let’s take a look at the methods that exist for the Random class. The command for doing so is:

[Random].GetMethods() | Select-Object Name –Unique

You can see the list of available methods shown in Figure B.

Image
Figure B: There are seven unique methods to the Random class.

It’s actually pretty easy to use the methods that are displayed. At its simplest, you will need to enter the class name (in brackets), followed by two colons, and the method name. Let me give you an example.

Pretend for a moment that for whatever reason, you needed your PowerShell script to calculate the value of PI. Well, it just so happens that there is a PowerShell class called Math and it has a method called PI. Therefore, you could get the value of PI by entering:

[Math]::PI

You can see what this looks like in Figure C.

Image
Figure C: You can use PowerShell to return the value of PI.

OK, by now you have probably noticed that I started out talking about the class named Random, but when I wanted to show you how to use a class and a method I used the Math class instead of the Random class. There is a method to my madness.

As you will recall, I promised to keep the .NET lesson as simple as I could. I couldn’t think of an easier example than calculating PI. Now that you know the basic syntax for using .NET methods we can look at using some other methods.

Conclusion

In this article, I have shown you the basics of using .NET methods within PowerShell. In my next article, I will show you how to use the Random method. In doing so, I will also show you how you can use MSDN to get help with a method’s syntax.

If you would like to read the other parts in this article series please go to:

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top