This video explores the difference between functions and scripts and how both can be used to make redundant and complex tasks simpler in PowerShell.
- [Instructor] PowerShell is usually defined in one of two ways. The first definition is that PowerShell is a command-line management and administration environment. The Cmdlets that we've looked at so far support that definition. If you were to open PowerShell and run the Cmdlet, Get-Command, you will see a list of all of the Cmdlets currently available to you, and it's a pretty lengthy list.
If we scroll up a little bit in this list, we'll find out that we have Cmdlets to uninstall Windows packages, also to stop or suspend Windows services. A little farther up, we could even stop or shut down this computer or another one, and we could continue to scroll through this list and find all kinds of functionality built right into what PowerShell currently knows how to do. As you look through these, though, you may find that while we have the ability to create a new local user, we don't have the ability to create a new active directory user, and there may be any number of other functions that as you look through, you'll notice seem to be missing.
Keep that in mind, because we're going to come back to that later. I mentioned that PowerShell is often defined from one of two perspectives. While this command-line environment is gradually replacing the traditional command interpreter, that's the smaller part of what PowerShell is. The other common definition is that PowerShell is a scripting language with all of the features you need to automate all aspects of computer management and network administration.
Far beyond the capabilities of DOS batch files and the at command to schedule tasks, PowerShell allows you to script and automate everything from creating users and restarting servers to just about anything you can program using the .NET framework or .NET core. As you become more familiar with PowerShell Cmdlets, you will find that, one, there are tasks that you do over and over again, and two, there are Cmdlets missing that you wish you had.
These often go hand in hand. In PowerShell, you can create scripts or ASCII text files made up of a series of Cmdlets that will complete a complex or repetitive task. More complex tasks may require defining variables and storing information possibly using loop operations like for each and while. These scripts are all saved to the hard drive or some other storage device as a separate file with the .ps1 extension, and they can be run from inside PowerShell by giving a path and the file name of the script.
These scripts can be anywhere from just a few lines long, to hundreds of lines long, depending on the complexity of the task to be completed. When you build a really long complex script, it becomes important to look for repetition. Sometimes you will find a series of a few lines of code that happens over and over again in your script. Sometimes, you will find that you use a Cmdlet with the exact same set of parameters repeatedly throughout one script.
There is a feature that's been around in scripting and programming for a long time to help with both of these situations. Functions are a great way to handle tasks that repeat within your script, and simplify your code. As we move forward, we're going to take a look at how to create one of these functions within a PowerShell session, as well as within a script.
- Getting started with PowerShell
- Using PowerShell cmdlet syntax
- Creating functions
- Working with PowerShell profiles
- Extending PowerShell with modules
- Managing pipeline output
- Creating PowerShell scripts in ISE