Functions are flexible and useful for a variety of tasks in PowerShell. In this video, we'll take a look at how to create a basic function and call it from the command line.
- [Voiceover] Let's start taking a look at some of the additional functionality that PowerShell can give you. Now one of the things you may have noticed when I ran Get Command, in an earlier video, I had these commands, I mean they're at the very top, and you can see them, they flash by really quick, called Functions. Now, Functions actually provides some useful capabilities inside of PowerShell. Now we see them just as commandlets, and we actually access them just like we would any other commandlets, and we see, Set Network, Adapter, or Set Net Adapter, or Set File Share, you see all these different things. And for us they are functions, and that's how we work with them, and how we call them.
In reality, underneath the cover, functions actually have a wide variety of commands that they actually do and run, for us. So you don't really have to worry so much about the built in functions, 'cause you work with them just like any other commandlet. However, you can also make your own functions that perform a variety of tasks. So, what I'm gonna do here is just make a quick little function, to add up a number. So you can create functions for mathematical equations, or for whatever parameters. I'm going to step you through the process, of how to just make a simple function, that adds two and two together. To give you a general idea of just one of the other areas you can really dig into, with PowerShell to add some additional functionality especially if you do mathematical equations, you're trying to date and time stamps, that kind of stuff, you're gonna create functions.
Now folks, just a clue, date and time in mathematical equations, there's a whole truckload of examples on how to do this, so you don't even have to reinvent the wheel, but I just want to show you a basic one here. So I'm just simply go, function, we'll call this Add. And notice that PowerShell here, is actually waiting for me, to type in more information here, it's actually waiting for me to fill in the parameters, for us to use. So I'm just gonna actually type in Enter and it's gonna actually bring up, saying, "Hey." Whenever PowerShell you see those three brackets, or those three arrows, PowerShell is saying you got more to do here.
There's more work that has to be done. So I'm gonna simply say, bracket here, to start this process, I'm gonna assign a variable called add, I'm gonna make that equal two, an integer, just so it's a number, and then I'm just gonna have two plus two, and I'm gonna hit enter again, and I'm gonna do what is called write the output of this. And what's that gonna do for me is just display the result of whatever that variable is, so I'm just going to write the output, and generally speaking, when you write an output, I'm always kind of cautioned, although I can probably get away without the quotes, I always like to put the quotes in just to make sure it's there.
And I'm going to close that bracket, there, go ahead an enter, and notice that my red arrow went away, so for all intense purposes, PowerShell thinks I'm done with this function, and I should be. As long as I didn't make any other typos, I'm just going to hit enter. And I'm just going to call my function, I just made a brand new one, I'm going to call it Add, and it's gonna display the result of two plus two, which is four. So the great thing about functions here, is that you can create your own, to do whatever you needed. Now, with that said, when you're starting to learn PowerShell, I don't know if you're really gonna dig into functions, but I like to point out there's a subtle different between what functions do, and what commandlets do.
But we call them exactly the same way, just know that it covers that function for like Set Net Adapter that we saw, might actually have several things it does inside of it. So even though we only call it a very simple name with some parameters, in reality, it's probably doing a truckload of work underneath. So here was just a quick peek at functions, and how they can work, and how to make just a basic one.
Matt then dives into PowerShell's functions and What If statements, working with output, and coding in the Integrated Scripting Environment (ISE). The course wraps up with some tips on using PowerShell for both on-premises and cloud deployments involving Office 365 and Azure.
- Reading the language
- Discovering cmdlets and aliases
- Using PowerShell functions
- Working with output
- Finding and installing modules
- Using PowerShell with Office 365 and Azure