Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Windows Server 2012 R2 has thousands of PowerShell cmdlets built into the operating system. As if that were not enough,
PowerShell is extensible. Microsoft provides modules that add additional PowerShell cmdlets that allow you to perform more
specialized tasks. Furthermore, Microsoft server products that are based around PowerShell also have their own sets of cmdlets. You
can even develop your own custom cmdlets if you want.
Since there are thousands of native PowerShell cmdlets and many more cmdlets that can be added onto PowerShell, one cant help
but to feel a little bit intimidated and to wonder why Microsoft has created such a large number of cmdlets.
There are a couple of different reasons why so many cmdlets exist. The first reason is that Microsoft wanted to keep things simple. I
know that sounds crazy, but hear me out.
If you have worked with some of the native Windows command line tools (executable files, not PowerShell), you probably know that
some of the tools are really complicated to use. A command line executable might have a long list of switches that can be used in an
effort to accomplish numerous different tasks. This is precisely the sort of thing that Microsoft tried to avoid when they created
PowerShell.
Rather than creating a set of complex, multi-purpose commands, Microsofts vision for PowerShell cmdlets was to create super
lightweight commands that do one thing, and one thing only. Since each cmdlet performs a very specific task, the cmdlets can be
linked together to perform more complex, multi-step processes.
Now admittedly, many PowerShell cmdlets do support the use of optional parameters. Even so, most of the cmdlets are fairly
straightforward to use.
The other reason why there are so many native PowerShell cmdlets is because of the way that the basic syntax works. As you will
recall, each cmdlet is made up of a verb and a noun. Each verb has multiple uses, as does each noun. For example, there are dozens
of cmdlets that use the Get verb. In other words, verbs and nouns are designed to be modular so that they can be used together in
various combinations. Each combination constitutes a different cmdlet. Thats why there are so many cmdlets.
Conclusion
In this article, I have spent quite a bit of time talking about the basic syntax used in PowerShell cmdlets and the reasons behind that
syntax. In Part 2, I want to begin showing you how to actually use some PowerShell cmdlets.
Introduction
In the previous article in this series, I talked about the basic syntax used by a PowerShell cmdlet and I talked about Microsofts
rationale for constructing cmdlets in this way. For those who might be wondering why I started out talking about something so basic, I
did it because it fits within my primary goal for this article series.
My hope for this series is that I can help you to feel more confident about using PowerShell and get rid of PowerShell related anxiety.
The reason why I started out by talking about the basic cmdlet syntax was to illustrate that even the most complicated PowerShell
scripts are really nothing more than a long series of simple tasks.
Obviously, knowing the basic cmdlet syntax is just a starting point. There are some other things that you need to know in order to
become proficient with PowerShell. That being the case, my plan is to compile a list of some of the most common questions about
PowerShell and then answer those questions. Some of the questions that I want to address in this article series are:
By the time that I have finished answering these eight simple questions, you should have a solid working knowledge of PowerShell. Im
not saying that you will be an instant PowerShell expert, but by understanding the answers to the questions above, you should be able
to accomplish just about anything necessary in PowerShell.
Advertisement
Maybe it would be more helpful to get a list of disk related cmdlets. Since we are still trying to retrieve disk related information, we can
include the Get verb in our query. So, if you wanted to see all of the cmdlets that use the Get verb and some variation of the word disk,
you could use the following command:
Get-Command Get-*disk*
As you can see in Figure B, you get a much shorter list of results this time. Out of the list of results, the only cmdlet that really seems
plausible is Get-PhysicalDisk. If you look at Figure C, you can see the error message produced by the Get-Disk cmdlet and the results
returned by using the Get-PhysicalDisk cmdlet.
Figure B: These are the only cmdlets used for retrieving disk information.
Figure C: The Get-Disk cmdlet produces an error while Get-PhysicalDisk provides the information we wanted.
In my experience, it is usually possible to use the Get-Command cmdlet to locate the cmdlet that is required for a given situation.
Every once in a while however, the required cmdlet is something really obscure that I never would have guessed. Fortunately, there
are ways to get help in those situations.
Lets pretend for example that I wanted to use PowerShell to delete a storage pool, but dont know what cmdlet to use. My first guess
would probably be to use the Delete-StoragePool cmdlet, but that guess would be wrong. The Delete-StoragePool cmdlet does not
exist.
Next, I would probably use the trick that I showed you a moment ago and enter Get-Command Delete-*However, this produces no
results. Clearly, Delete is not the right word.
There are a few different things that can be done in this situation. One option is to try to use the Get-Command cmdlet against the
word StoragePool. The command for doing so is Get-Command *-StoragePool. In this case, PowerShell shows us four related cmdlets
and it is easy to tell that the cmdlet that we are looking for is Remove-StoragePool. You can see the results in Figure D.
OK, so that trick gave us the information that we needed, but what if it hadnt? What else could we do? Well, one option is to use the
Get-Help command. Since we know that we are trying to do something storage related, we could type Get-Help Storage. Doing so
would provide us with a list of storage specific commands, as shown in Figure E.
Introduction
In my previous article, I listed eight questions with the goal of helping my readers to feel more comfortable with PowerShell by
answering those eight questions. I answered the first question in the previous article, so now I want to move on to answering some of
the other questions on the list.
Advertisement
How do I Specify Parameters for a PowerShell Cmdlet and How Can I Get Help with Using a
Cmdlet?
Many PowerShell cmdlets can be used without the need for additional parameters (at least in certain circumstances). The Get-VM
cmdlet for instance, returns a list of Hyper-V virtual machines without you needing to specify any additional parameters. Often times
however, you will find that you need to specify one or more additional parameters in order to get a cmdlet to do exactly what you need
for it to do. So how do you know which parameters you can use with a given cmdlet?
There are a number of different techniques that you can use, but my advice is to take advantage of the Get-Help cmdlet. To use this
cmdlet, simply type Get-Help, followed by the cmdlet that you need assistance with.
To show you how this works, lets pretend that you needed to retrieve a list of the physical disks that had been previously retired from
your server. The Get-PhysicalDisk cmdlet is used to retrieve physical disk information, but how do you get information about retired
disks?
The first step in the process would be to get help on the cmdlet. The actual command that you would enter is:
Get-Help Get-PhysicalDisk
When you enter this command, PowerShell will display the full syntax of the Get-PhysicalDisk cmdlet. It is common for this syntax
information to be followed by usage examples, although Microsoft does not provide examples of how to use Get-PhysicalDisk. You
can see what the syntax information looks like in Figure A.
The next thing that we see after Usage is <usage[]> this indicates that the Usage parameter requires some additional information.
You cant just use the command Get-PhysicalDisk Usage because even though Usage is a valid parameter, PowerShell needs more
information. It needs to know what kind of usage you are interested in. Any time you see a word in between the less than < and
greater than > signs, it indicates that you are going to need to provide some additional information to the parameter. So in this case, Usage is the parameter and <usage[]> is the additional data.
Of course this raises the question of where we can find this additional data. Well, it is included in the command syntax. You will notice
that the next section of the syntax is:
{Unknown | AutoSelect | ManualSelect | HotSpare | Retired | Journal}
You will notice that the list of words is enclosed in braces {}. These braces indicate that the words within can be used in conjunction to
the optional parameter. To show you what I mean, lets go back to the original example in which I wanted to find information related to
retired physical disks. To get this information, I could use the following command:
Get-PhysicalDisk Usage Retired
So you can see that I am appending the Usage parameter to the Get-PhysicalDisk cmdlet, and then I am telling PowerShell what
type of Usage I am interested in.
The syntax that is displayed within the above figure continues on, but the basic pattern repeats. The syntax information lists a number
of optional parameters and the data that can be used with those parameters. Some of the other optional parameters for instance are
Description, -Manufacturer, -Model, -CanPool, and HealthStatus, and the list goes on.
Variables
Abbreviations
Operators
Comments
Scopes
Im not going to talk about scopes in this article series because scopes are a more advanced topic (I plan on eventually writing a
separate series that covers advanced scripting), and variables are going to be discussed later in this series. For right now though, lets
take a look at abbreviations, operators, and comments.
Comments
Comments are simply lines of readable text that are inserted into a PowerShell script for documentation purposes. A comment always
begins with a pound sign (#). Everything after the comment is ignored by PowerShell. Comments only exist as a way of documenting
what a script does. Here is an example of a PowerShell comment:
# This is a comment line.
Abbreviations
PowerShell abbreviations are simply a way to shorten a PowerShell cmdlet so as to reduce the amount of typing required. I have to
admit that PowerShell abbreviations are a bit of a pet peeve for me. I almost always prefer to write PowerShell code in long form
because I think that doing so results in code that is more readable and easier to understand. Even so, abbreviations are common
enough that it helps to familiarize yourself with them.
Let me show you an example of how a PowerShell abbreviation works. Suppose for a moment that I wanted to create a list of all of the
virtual machines that exist on a Hyper-V server and then format the information in table form. I could accomplish this task with the
following command:
Get-VM | Format-Table
The Get-VM portion of the command retrieves the list of VMs. The next thing that you see is the pipe symbol (|). The pipe symbol is
used to combine two cmdlets. The pipe symbol causes the output from the first cmdlet to act as input for the second cmdlet. So in this
case, we are taking the output from the Get-VM cmdlet and using it as input for the Format-Table cmdlet, which controls how the
resulting data is displayed. You can see what this looks like in Figure A.
Figure A: The Get-VM | Format-Table command creates a table displaying Hyper-V virtual machines.
So now that I have shown you how the Get-VM | Format-Table command works, lets get back to the discussion of PowerShell
abbreviations. The Format-Table cmdlet is often abbreviated FT. Hence, the command could be shortened to:
Get-VM | FT
As you can see, the abbreviated command barely resembles PowerShell. As you can see in Figure B, the abbreviated command
delivers the same output as the long form command. Another example is that Format-List is often abbreviated as FL.
Operators can be another source of confusion within PowerShell. Operators are comparative functions such as greater than, less than,
or equal to. The reason why operators are sometimes confusing is that the most common operators are expressed in an abbreviated
form.
The operators that are used within PowerShell include:
EQ (Equal)
NE (Not Equal)
GT (Greater Than)
LT (Less Than)
Like
NotLike
Match
NotMatch
Contains
NotContains
In
NotIn
Replace
Admittedly, some of these operators are more commonly used than others. The most commonly used operators tend to be EQ, -NE, GT, -GE, -LT, and LE.
Since operators are such a big part of PowerShell, I want to take a few minutes and show you how some of these operators work.
PowerShell operators are used as a question. For example, you can ask if A is greater than B. The answer is sometimes Boolean
(True / False) or it may be numerical. PowerShell is usually smart enough to figure out which type of answer makes the most sense.
Suppose for instance, that I wanted to know if 6 was greater than 8. I could pose the question as:
6 GT 8
PowerShell would return an answer of false. Now suppose that I wanted to provide PowerShell with a list of numbers and then
determine which of those numbers was greater than 8. I could use a command like this one:
6, 7, 8, 9 GT 8
In this case, PowerShell would give me an answer of 9 rather than giving me a True / False answer. You can see an example of this in
Figure C.
You can also treat the Like operator as a which one is like operator. Suppose for example that I wanted to know whether the word
Windows or the word PowerShell was like the word Power. I could find out by using a command like this one:
Windows, PowerShell Like *Power*
The response should be PowerShell. You can see an example of this in Figure D.
Conclusion
In this article, I have explained why some blocks of code might not look the way that you would expect PowerShell code to appear. In
the next article in this series, I will continue the discussion by showing you how variables work. From there, we will move on to building
some PowerShell scripts.
First, you can use variables without first assigning them. I took several programming classes in school and many of the programming
languages that I studied required variables to be declared within a block of code at the beginning of the program. PowerShell doesnt
have this requirement. You can use a variable on a whim.
The other thing that PowerShell does to make your life easier is to precede every variable with a dollar sign ($). This makes it really
easy to spot variables.
So with that said, there are two primary methods for assigning a value to a variable in PowerShell. The first method involves using the
Set-Variable cmdlet. To show you how this works, lets pretend that I wanted to create a variable named $UserName and I wanted to
assign it a value of Brien. I could accomplish this by entering the following command:
Set-Variable Name UserName Value Brien
As you look at this command, you will notice that it doesnt make use of a dollar sign. The reason for this is simple. Because I used the
Set-Variable cmdlet, PowerShell was smart enough to know that it needs to add a dollar sign in front of the variable name.
So what about retrieving the value of a variable? Well, thats super simple. All you have to do is type the variable name. In this case,
we could find the variables value simply by typing $UserName. You can see how this works in Figure A.
Figure B: PowerShell allows you to assign a value to a variable without using the Set-Variable cmdlet.
Before I move on, I need to take just a second and talk about a couple of rules for variables. First, you will notice that I assigned two
different data types. I showed you an example of assigning a string value (Brien) and an example of assigning an integer value (4).
PowerShell is actually really flexible when it comes to variable assignments. Windows doesnt seem to care if you are assigning text,
numbers, or even blocks of code to a variable.
One thing that you do have to pay attention to however, is the variable name. There are certain words that you cannot use as a
variable name because the words are reserved by PowerShell. These are words that you can use in PowerShell without using a
cmdlet. For example, in the next article in the series I am going to show you how to create PowerShell functions. The word Function is
reserved and cannot be used as a variable. The same also applies to words such as If, ForEach, and Return.
So how can you put a variable to work? As you will recall, I showed you some of the operators that are supported by PowerShell in
one of the previous articles. These same operators can be used to compare the contents of a variable. This obviously isnt the only
way that you can use a variable, but it makes a good example.
Earlier, I set a variable called $X to a value of 4. Lets do a really simple comparison to see if $X is greater than zero. If it is bigger than
zero, we will type Bigger than Zero as a response. So here is the line of code that we will use:
If ($X GT 0) {Bigger than Zero}
At first glance, this looks like one of those really cryptic blocks of code that I talked about in the previous article, but there is a method
to the madness. This line of code is essentially an if then statement.
There are three parts to the command. The first part is the word if. This tells PowerShell that we are doing an if then.
The second part of the command is ($X GT 0). This is our comparison. $X is our variable. GT is a PowerShell operator that means
greater than. 0 is the value that we are comparing against. So in essence this part of the command is checking to see if the value
within variable X$ is greater than zero.
The last part of the command is {Bigger than Zero}. This simply causes the text Bigger than Zero to be displayed if the condition in
the second part of the command is true. Therefore the command could be summarized as if $X is greater than 0 then write Bigger than
Zero. You can see the command in action in Figure C.
Conclusion
In this article, I have explained how variables work within Windows PowerShell. In Part 6, I plan to conclude this article series by
explaining how functions and scripts work within PowerShell.
those commands into a script by copying them to Notepad and then saving the file with a .PS1 extension. For the sake of
demonstration, I called the script MyScript.ps1.
Figure B: You can use the Get-ExecutionPolicy cmdlet to verify the execution policy that is in effect.
Once you have verified the execution policy, you can run the script by navigating to the folder containing the script and then entering ./
followed by the script name. To show you what I mean, lets run the MyScript.ps1 file. I saved my script to C:\Users\Brien\Documents.
Therefore, the commands that I would use to change to the correct folder and then execute the script are:
CD \
CD Users\Brien\Documents
./MyScript.ps1
You can see that this looks like in Figure C.
Figure C: You can use ./ followed by the script name to execute a script.
So now that I have shown you how to create and execute a script, lets turn our attention to PowerShell functions. A function is a block
of code that can be called by name. Normally, a function exists within a script and is used whenever a particular block of code needs
to be used more than once. Of course this isnt the only way to set up a function. A function can be used outside of a script. Since
functions normally reside in scripts however, lets modify our script to include a function.
I want to try to keep things simple, so I really dont want to add a big, complicated block of code to our script. Instead, lets use some
imagination. Lets pretend that our script is actually much longer than it really is. Lets also pretend that through the course of running
the script, the value of $X changes several times and that we need to periodically check to see if the variables value is greater than
zero.
In this type of situation, we could write an IF statement every time that the variable is assigned a new value, but that would result in a
lot of repetitive code. I always think it is better to write less code whenever possible because shorter code blocks are less prone to
coding errors.
So with that in mind, lets place our IF statement inside a function. Since functions have to be named, I will call the function VarCheck.
To declare a function, all we have to do is enter the word Function, followed by the function name and the code block that we want to
include within the function. The code block must be enclosed in braces {}.
So lets place our IF statement into a function called VarCheck. The function would look something like this:
Function VarCheck
{
If ($X -GT 0){"Bigger than Zero"}
}
Any time that you want to call the function, you must simply enter the functions name. If for example, we set the value of $X and then
wanted to check to see if $X was greater than 0, we could use the following lines of code:
$X=4
VarCheck
The entire function, with the script looks like this:
Function VarCheck
{
If ($X -GT 0){"Bigger than Zero"}
}
$X=4
VarCheck
Obviously this is a really simple example, but it illustrates the way that a function works. If we wanted to reassign and recheck the
value of $X a few times, we could do something like this:
Function VarCheck
{
If ($X -GT 0){"Bigger than Zero"}
}
$X=4
VarCheck
$X=0
VarCheck
$X=1
VarCheck
You can see the output of this script in Figure D.
Conclusion
I had originally intended for this to be the last article in my PowerShell essentials series, but I am going to write one more. The reason
for this is that in the real world it is sometimes necessary to pass parameters to a function and I didnt get a chance to talk about that.
Therefore, I want to spend Part 7 talking more about functions.
Lets pretend that I need for my script to do the same thing that it does now, but that for whatever reason I want to pass values directly
to my function rather than making use of a variable. If that is the goal then the function would need to be modified to look like the block
of code shown below:
Function VarCheck($arg1)
{
If ($arg1 -GT 0){echo $arg1 " is bigger than zero"}
}
VarCheck 4
VarCheck 0
VarCheck 1
This script pretty much does the same thing as the script that we created before, but there are two differences. The first difference is
that now we are passing values to the function rather than declaring variables within the main body of code. The other difference is
that I have changed the output slightly as a way of making the output more clear.
With that said, lets step through the code and see how this script works. My first line of code defines a named function. Just like
before, I am calling this function VarCheck. The thing that is different from my previous script is that the function name (in this case
VarCheck) is followed by: ($Arg1)
This is how we tell PowerShell that we are going to be passing a parameter (sometimes called an argument, hence the Arg1 name) to
the function. Now if you look closely, you will notice that $Arg1 is a variable because its first character is a dollar sign. This variable
automatically inherits whatever value is passed to the function. Therefore, if we passed a value of 4 to the function then $arg1 would
contain a value of 4.
The second and fourth lines of code contain a set of braces ({}). Remember, the code block that is associated with a PowerShell
function is always enclosed in braces. These braces define the beginning and the end of the functions code.
This particular function only uses a single line of code. Its the line that appears between the two braces. This line if very similar to
what we were using in the original function, but there are two changes. To more clearly illustrate those changes, let me show you both
the old and the new version of this line of code:
This is how the line originally looked:
If ($X -GT 0){"Bigger than Zero"}
This is how the line looks now:
If ($arg1 -GT 0){echo $arg1 " is bigger than zero"}
In both cases the line of code is checking a variable to see if it is greater than (-GT) zero. In the original version, the code checked the
value of $X. As you will recall however, our new script doesnt use the $X variable. We are passing values directly to the function and
the function assigns those values to the variable $arg1. Therefore, the new function compares $arg1 to 0 rather than comparing $X to
zero.
In both lines of code, we are essentially performing an if then. Both lines of code begin with an if and then establish a condition. The
condition is enclosed in parenthesis and in each case compares the value of a variable against 0. The next part of the line is the
then. This is the portion of code that is enclosed in braces. Its what happens if the condition is true.
In our original code, the then portion of the line simply displayed the text Bigger than Zero. In the new version of the code, I have
made a slight modification that displays the value of the $arg1 variable before displaying the Bigger than Zero text. I did this by using
the word Echo, followed by the variable name. If you have ever done DOS batch programming, you may recall that Echo is a
command used to display a line of text.
Now look below the function. You can see that all of my function calls are followed by a value. For example, VarCheck 4 passes the
value of 4 to the function, where it is compared against zero. Although I am passing raw data to the function, you can use the same
technique to pass a variables contents to a function. You can see the output of my script in Figure B.
Conclusion
In this article, I have explained how you can pass values to a PowerShell function. In my next article, I want to show you how you can
use PowerShell to tap into the power of .NET.
Figure A: The COPY CON command works in the Windows Command Prompt window.
Now check out Figure B and you will see what happens if you try the same thing in PowerShell.
Conclusion
I had originally planned on discussing .NET in this article, but will cover it in Part 9 instead. For those of you who are not developers,
let me assure you that leveraging the .NET environment through PowerShell is surprisingly easy to do. I will show you how in my next
article.
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.
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, lets 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.
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 methods syntax.
This article concludes the PowerShell for Beginners series by talking more about PowerShell and .NET
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 arent always quite that simple. The nice thing about the PI method is that we were able to
use it as a standalone object. We didnt have to provide any additional information. Using other classes and methods isnt 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 isnt 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
wouldnt 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 Microsofts 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. Its 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 isnt among the languages listed, but that isnt a problem.
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.
Figure B: Entering [Random] produces a bit of information about the Random class.
Another thing that we can tell right away is that you cant 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. Its not going to happen. To put it another way, a class is nothing but a classification. Its 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.