Interface Technical Training

How to Build in a PSMethod to your PowerShell Code

In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that’s added into the PSObject.

For instructor-led PowerShell courses, see our course schedule.

Microsoft Windows PowerShell Training

Download the Building Methods PowerShell script</a> used in this video.


Video Transcription:

Hi, I’m Jason Yoder with Interface Technical Training. Today, we’re going to take a look at how to add methods into your code. Let me describe to you what a method is.

When we’re working with objects, we have properties. Those describe what this object is. We also have methods, and that’s free software that’s added into the object. If there’s some functionality you want to give to your object, to whatever you’re representing, you can do that in code. Let me give you an example.

[00:30] I’m going to open up an instance of Notepad, and I’m going to minimize it. You can see it’s still running down here in my taskbar. I’m going to run Get‑Process, and I’m going to get the process object that represents that instance of Notepad. Next, I’m going to pipe it to Get‑Member, and I’m going to ask for the methods of this particular object.

[00:50] Two things I want to point out to you. First of all, we’re going to use the kill method, but I need to know how to use this kill method, so I need to go online and look at the documentation on MSDN. To find the documentation, I scroll up here to the type name, and this is what I’m going to be searching for, System Diagnostics Process. This is the unique name in the .NET Framework that describes this particular object.

[01:16] I’m going to go ahead and go directly to MSDN. We’ll be able to see what this particular method does. Let’s scroll on down to the methods section. What we can see is “kill immediately stops the associated process.” That’s what we want to do, we want to stop that process.

[01:38] What I’m going to do is I’m going to run Get‑Process once again. I’m going to grab the unique object that’s focused on Notepad. You’ll see it’s inside parentheses. That way, I can do .kill. In other words, I can call the method. Since this method does not receive any arguments, you notice that the parentheses after it is empty.

[01:59] When I run this code, take a look down here at my taskbar, Notepad terminates. That was code that was built inside of the actual objects. We can take advantage of it. We’re going to learn how to do that with our code.

[02:12] First of all, we’re going to start off with a basic function. This function, it’s called Test Method1, is going to accept the parameter called value. We’re going to create a new object. There’s many different ways to create objects. I’m just showing you just one way of doing it.

[02:29] You can see it’s going to create a new object that’s going to contain a single property called value and whatever number we give it, and store it in a variable called object. We’re then going to use the PowerShell cmdlet right output to place our object into the PowerShell pipeline.

[02:45] I’m going to go ahead and run this code in the memory, and now we’re going to call it. You can see we have an object, it has a property called value, and it’s currently set to 10. I’m going to send that to Get‑Member just to show you that we do in fact have an object and there’s our property. All right. We have our basic object created. Let’s go ahead and add in a method.

[03:05] The first method type we’re going to use is one that does not accept any arguments whatsoever. I’m going to focus your attention to this part of the code. This is the only thing that we’ve changed. You can see I’m taking that variable OBJ that I created earlier that’s storing our current object, and I’m piping it to Add‑Member.

[03:24] Again, there are many different ways that we can do this. This is just one way of achieving our goal. The member type, this is a script method, we’re creating the method inside of our script. The name, we’re going to call this clear value. Then we come to the next part, the value of this particular method.

[03:42] Now, you see it’s inside of our curly braces, our script lock. Remember that can be multiple lines of code. In our case, we only need a single line. I need to describe to you what this variable does. This represents this object right here. Me. As we know, our object has a property called value. We created that property up here.

[04:04] Essentially, what we’re going to do is we’re going to say, “Take the object that I’m a part of, look at my value property, and let’s set it to 0That’s all we’re doing here. I’m going to go ahead and read this in the memory.

[04:17] Let’s go ahead and test its functionality. It still works. When I pipe it to Get‑Member, not only do we have our value property, but we also have a method called clear value associated with it. I’m going to go ahead and run our cmdlet again, but this time I’m going to save our output into a variable called data. Just makes a lot more cleaner to work with.

[04:38] When we run data, you can see our object’s still present. Now I’m going to call the method that we just added into the code. Again, this method does not take an argument, so the parentheses are going to be empty. We must have parentheses when we’re using methods.

[04:53] I’m going to ask for the contents of data once again, and you can see it work. We’ve cleared our data. It’s now 0That’s how we added in a method that does not accept the value. Let’s add one in that does accept the value.

[05:09] Here we have our code again, but this time we’re going to look at this section of the code. This is what we’ve now added in. I’m doing a second method. We’re keeping the original. This new method is going to be called Add‑Number. This is how you tell it, “I am going to accept a value.” You create inside of this script block a parameter statement, and we give it a variable, number.

[05:34] This is how we use. What we’re going to do is we’re going to take the current value of whatever the number is stored in our value property, we’re going to add this number to it. Here you can see this object’s value property is now going to be assigned its current value, because we don’t want to get rid of it, plus our number. Whatever we send to it. That’s what’s going to happen.

[06:01] Let’s go ahead and put this function in the memory. Let’s test it once again. We can see it’s 10. If we run this against Get‑Member, we can see there’s our Add‑Number method that we just added in. I’m going to go ahead and place it back into a variable. The value is 10. We’re going to go ahead and call our method.

[06:24] Since we do have a argument associate with it inside the parentheses, I have to give it a value. We added in our number 5 to 10 and we check our data. We can see it added it together.

[06:37] By adding methods into your code, it’s a way of providing extra functionality to the user in the form of a method. The real neat thing about this is that it is part of the object and it will continue through the PowerShell pipeline as long as that object is in existence. You can add in whatever functionality you need.