Chapter 19. From command to script to function
Let’s say that you’ve come up with a great command that you not only want to use again and again, but you want to share with your colleagues and co-workers. It’s a somewhat complicated command, so you want to put it into a batch file for them. There are also one or two things that need to change each time you run the command, such as a computer name or other parameter, so you want to make it easy for them to provide that information. PowerShell makes it easy to do all of that, and you’re going to see how in this chapter.
If I’ve said it once, I’ve said it a million times: you don’t need to be a programmer to do amazing things with PowerShell. Given a command that does what you want, you just need to add a little bit of structure—not really programming or scripting code—to modularize it and to start making a reusable tool out of it.
Let’s start with two commands. We’ll get a computer’s name, operating system build number, service pack version, and C: drive free space in megabytes (MB). We can do that right from the shell by using these two commands: