Learn PowerShell Scripting FAST
- select the contributor at the end of the page -
We've covered the basics of how PowerShell works, where it comes from, how to use the help file and how to run simple PowerShell cmdlet commands. If you don't already know all of those pieces, you'll want to start with PowerShell basics covered in the PowerShell Fast Crash Course.
Powerful PowerShell Cmdlets
The truth is that most PowerShell commands do something that you can do elsewhere. For example, when we used the stop-service cmdlet to stop the Print Spooler service, we could have done the same thing via the Administrative Tools graphical interface. While using the PowerShell command line is cool, admins need more than just a way to show off in front of Junior Windows Administrators.
One way to get more power out of PowerShell is to build scripts that run multiple cmdlets. However, there is plenty of power lurking behind even single PowerShell scripts to both be useful, and to impress the nearest Junior Admin.
Let's say that, for some reason, you need to stop all of the services running on a machine whose names start with SQL . Sure, you could open Services and click each service, then click Stop, and then wait for it to stop, but what if you need to do that on a dozen machines in the data center? It can be done but it will be very tedious. PowerShell scripting language to the rescue!
To defeat the tedium, open up the PowerShell ISE (Remember to run it with administrator privileges).
If you need to review how to use the stop-service cmdlet, check out its help entry. (get-help stop-service)
Type stop-service –DisplayName SQL* -force
The DisplayName parameter means to use the name that shows up under "Name" in the Services administrator tool. On my current machine, that is five different services. The asterisk is a wild card just like in many other programming languages. It means to match anything that comes after SQL, so we are going to stop all the services that start with SQL.
After running the command, all five services have been stopped. No point, click, wait, required.
Most Powerful PowerShell Feature
Before we go any further, it is time to cover PowerShell's single most powerful feature.
The most important PowerShell switch any administrator can know is the PowerShell WhatIf switch.
The WhatIf switch is available on most cmdlets that actually affect a system. For example, the get-help cmdlet does not support the –whatif switch, but the stop-service command certainly does.
When we stopped all of those services, we blindly used the SQL* to stop them. What if there was one we forgot about, one that was really important and that we didn't want to stop?
The WhatIf switch can save administrators a lot of headaches.
If we typed stop-service –DisplayName SQL* -force –whatif
Then, the result of the command appears in the middle pane just like any other command, however, everything is preceded by a "What If:". It tells us nothing REALLY happened, but this is what would happen if you ran the command.
In this case, we see:
What if: Performing operation "Stop-Service" on Target… and a list of each of the services it is stopping. At this point you can review what is going to happen and be sure that it is really what you intended. If, for example, you see a service stopping that you don't want stopped, you can edit your command. In this case, you can use commas to separate service names and specifically stop the ones you want, rather than using the blanked asterisk character.
PowerShell Commands List Help
Now that you have a taste of what PowerShell can do, you'll need to find cmdlets to do your bidding.
There are hundreds of PowerShell cmdlets in the core implementation, plus hundreds more that can be imported into a given environment. If you want to get a big dose of many different PowerShell cmdlets, try some good PowerShell training. However, all PowerShell cmdlets follow a standardized naming convention that makes looking up, finding and using cmdlets easy for experienced administrators looking to tackle a new function with their PowerShell scripts.
First, all PowerShell cmdlets start with an approved verb, followed by a noun. For those of you that don't remember your English grammar classes very well, a verb is an action word. A noun is a "thing" word that typically refers to an object.
In our example, we have been using the cmdlet stop-service. "Stop" is the verb, or the action, that is imposed upon the noun, "service." All cmdlets work in the same way. You can search the help file based either on what you want to do: stop, start, get, add, remove, clear, copy, new, or based on what you want to affect: service, event, module, item, process, computer, and so on.
You can get a list of all the approved cmdlet verbs in PowerShell by entering the get-verb command. There is no get-noun command, however.
We've been using get-help to find cmdlets that we seek, but there is also a get-command function. The get-command function allows you to specify specifically which part of the cmdlet you are interested in by adding either the -noun or -verb switch to your command. For example, get-command -verb stop returns a list of cmdlets that begin, stop-. Likewise, get-command -noun process returns a list of cmdlets that affect a process and end with -process.
In many cases, get-help will get you a usable list as well. However, sometimes, get-help makes an assumption or is too broad to help. If you type, get-help start, PowerShell makes the assumption you want to know about the start-process cmdlet. If you type, get-command -verb start, you get a list of five different cmdlets. It is a good habit to use get-help to find out about how to use various commands and functions, and get-command to find specific commands.
One tip that will save administrators a lot of time when learning PowerShell is that the verb "get" is used for just about anything that involves reading, looking up, checking status, or listing. The idea is that you are "getting" whatever information you desire. So, whether it is a directory listing, the current state of a process or service, or even if something exists, the verb for the command is likely get.
Remember, computer programmers write PowerShell, not English teachers (or freelance writers), so be aware that non-verbs such as "new" show up from time to time in the verb position, even though they are not, strictly speaking, verbs.
Now that you see how PowerShell works and how to find the cmdlets you need, it's time to play around a bit. When you have a handle on using single commands, it's time to learn how to build more complex, multistep PowerShell scripts.
Interested in learning more about PowerShell? See our PowerShell v3 New Features course, PowerShell Fundamentals course and Windows Server 2008 PowerShell Training.