Windows has never enjoyed the powerful shell scripting environments that its Unix rivals have long included. That's changed now with the inclusion of PowerShell, Microsoft's command-line shell and scripting language, in Windows Server 2008 and a passel of other Microsoft server products as well.
Finally, Windows has a classy, robust and powerful solution that can access just about every part of the operating system. Indeed, some administrative consoles of recent Microsoft server products are simply GUI front ends for bundled PowerShell commands and scripts - such is the power of PowerShell.
If you've had a chance to play around with PowerShell a bit, read on for five tips, tricks and ideas for putting PowerShell to work in your daily computing life. There are code snippets, sample cmdlets (pronounced "command lets") and sample scripts from which you can build your own PowerShell solutions.
Ready, class? Off we go.
1. Create your own cmdlets in PowerShell
At the heart of PowerShell is the cmdlet, a cute moniker that refers to the simplest bit of .Net-based code you can execute that actually returns a result, either from the PowerShell prompt or from a script. For example, the Get-Process cmdlet lists all processes, while the Get-PSSnapin cmdlet shows all current PowerShell snap-ins that enable new functionality.
The real power comes from creating your own cmdlets, a convenient way to save any scripts that you have developed for use later or on other machines. For example, if you have a script that queries Active Directory, finds computer names and then records their service-pack level in a table, you'd want to save that and be able to run it at a later time, or perhaps even from a central workstation that could iterate that command over a range of computers.
To create your own cmdlets, follow these three easy steps:
A: Enable scripts to run By default, PowerShell won't let scripts run for security precautions. You can enable personal script execution by using the set-executionpolicy RemoteSigned command at the PowerShell prompt.
B: Create your scripts Just write your PowerShell commands in a text editor, like Notepad, and use line breaks to separate the commands. If you have written DOS batch files before, this is old hat. Make sure that you save the file name with a .ps1 extension, which indicates to PowerShell that the file contains a script.
C: Run the scripts from the command prompt You can also create an alias for your new script. If I wanted to run my Active Directory query script as mentioned above, and the script was located at H:\Scripts\AD-OSbuild.ps1, I could use NewAlias GetOSRevs H:\Scripts\AD-OSBuild.ps1 to create an alias that lets me just use the GetOSRevs command from that point forward to access the script. This saves time and finger fatigue.
2. Use robust loops in PowerShell
As we all know, loops are one of the most fundamental, but most powerful, tools of the trade for performing repetitive actions. PowerShell can support the following types of loops:
- While loops for setting criteria at the beginning of a loop.
- Do while loops for evaluating the criteria statement at the end of a cycle.
- Foreach loops to pull in items from a group of values (in PowerShell parlance, this is known as a collection).
- For loops or For statements to perform an action on a subset of values.