PowerShell Tips and Tricks

At last, Windows has a powerful scripting environment. Here are five real-world ways to put PowerShell through its paces

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.
You will see these loops in action in the code snippets further down in this piece.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Jonathan Hassell

Computerworld
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?