PowerShell Essentials (Part 10)

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

In my previous article, I began talking about how you can use PowerShell to tap into the power of .NET. Along the way, I showed you a couple of examples such as how to calculate the value of PI. In this article, I want to talk some more about your options for interacting with .NET.

As I explained in the previous article, interacting with .NET requires you to know about classes and methods. A class is basically a category of commands and a method can be thought of as an individual command. I am simplifying things, but for right now this is a safe way of thinking about classes and methods. If you think back to my PI example, the class was called Math. The Math class contains all of the math related functions (or methods). PI was the name of a method within the Math class. Using the PI method required us to specify the class name (Math) and the method name (PI). The actual command looks like this:

[Math]::PI

As I hinted in the previous article, things aren’t always quite that simple. The nice thing about the PI method is that we were able to use it as a standalone object. We didn’t have to provide any additional information. Using other classes and methods isn’t always quite so simple.

For the sake of discussion, I want to use the Random class to show you how to interact with methods. You got a taste of this class in the previous article when I explained that you could list the methods that exist within the Random class by using this command:

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

There are seven unique methods to the Random class including Next, NextDouble, NextBytes, ToString, Equals, GetHashCode, and GetType. But how do you use these methods? Clearly, this isn’t going to be as simple as displaying the value of PI.

I could jump right in and tell you exactly how to use the Random class and its corresponding methods, but I think that I would be doing you a disservice if I did. The reason for this is that Random is only one of many different classes that are available through .NET. It wouldn’t be practical for me to write about every class and every method. Likewise, it would not be realistic for me to expect you to memorize the syntax for every method in existence. So rather than just showing you how to use the Random class and its methods, I would rather show you what to do in a real life situation when you are faced with a class or a method that you do not know how to use.

In this type of situation, I recommend turning to MSDN. If you are not familiar with MSDN, it is the Microsoft Developer Network and is accessible at http://msdn.microsoft.com. MSDN is Microsoft’s go-to reference for developers and it contains all of the information that you will ever need about .NET and about PowerShell.

In my opinion, navigating MSDN directly can be a little bit cumbersome. It’s a lot easier to do a Web search on a phrase like “.NET random class”. The first result will usually be an MSDN document. If you look at Figure A for instance, you will see an MSDN document related to the Random class. This document shows you class syntax for a variety of programming languages. Unfortunately, PowerShell isn’t among the languages listed, but that isn’t a problem.

Image
Figure A: MSDN contains full documentation for .NET classes.

If you look at the very bottom of the figure above, you will notice that there is a section called Methods. The Methods section lists the methods that are associated with the .NET class. You can click on an individual method to learn more about it. If you click on the Next method for instance, you will see that it is designed to return a positive random integer.

Admittedly, the information on MSDN can be a little bit tough to follow if you are not a developer, so let me break it down for you. Like the Math class, if you want to use the Random class, you will have to enclose the class name in brackets. This holds true for any .NET class.

If you simply enter [Random] into PowerShell, you get a little bit of information about the Random class. As you can see in Figure B, we can see that the class is public, and that it is serial. We can also tell that the Random class is a system object.

Image
Figure B: Entering [Random] produces a bit of information about the Random class.

Another thing that we can tell right away is that you can’t just enter [Random] to get a random number. This is to be expected. After all, Random is a class name, not a function. Entering [Random] and expecting to get a random number would be like entering [Math] and expecting to see the value for PI. It’s not going to happen. To put it another way, a class is nothing but a classification. It’s the methods that do all of the work.

If you want to produce a random number, you will need to use the Next method. The basic syntax used for doing so is a little bit different from what you might be used to. The command used to generate a random number looks like this:

([Random]1).next()

In this command, the Random class is using the number 1 as a starting value and calling the Next method, which produces a random number. The starting value is called a seed value. It is used when you want to generate a predictable random number (for testing purposes). You can use any seed value you want.

Remember when I said that the Next method produces a 32-bit positive integer. A 32-bit positive integer is a relatively large number. If you use 1 for the seed value then the resulting random number is 534011718. If you want to have a bit more control over the output, you can use a command like this one to specify the output range. This command picks a random number between 1 and 100:

([Random]1).next(“1”,”100”)

Again, if you use a seed value then the command will produce identical results each time it is run.

One last thing that I want to mention is that just because I used the Random class to explain how to get help using classes and methods, does not mean that you have to use [Random] to generate a random number. Often times Microsoft has created PowerShell cmdlets that leverage .NET so that you do not have to do so manually. For example, there is a PowerShell cmdlet called Get-Random that retrieves a random number. If you wanted to generate a random number between 1 and 100, you could use this command:

Get-Random –Maximum 100 –Minimum 1

This command does not use a seed value and the results are therefore not identical.

Conclusion

In this article series, I have shown you the basics of working with Microsoft PowerShell. As you have seen, PowerShell gives you almost total control over the Windows operating system.

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

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top