PowerShell Tips and Tricks

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

5. Use object-oriented features in PowerShell

PowerShell is, at its core, an object-oriented product. In other words, all of PowerShell's features, syntax and components are based on programmatic objects. So cmdlets reveal their results as objects, variables in fact are objects, and the various syntaxes that make up PowerShell provide the ability to manipulate objects.

You can use the properties of an object, and you can call the methods an object contains. While some PowerShell users may not make much direct use of this object orientation, PowerShell lets you make use of .Net and WMI objects, both at the command line and in scripts.

Using .Net objects is pretty easy, since much of it happens by default. You can either let PowerShell determine the .Net classes by default - for example, typing LS or DIR in a file system drive produces a set of file and directory objects. PowerShell can also access COM objects, which is useful for accessing legacy applications and some parts of Windows.

An example of this is using WMI to examine the Vista Firewall. Using PowerShell, you can use the firewall COM object to obtain details of how the Vista firewall is configured. For example, the following two commands grab information on the firewall from the COM object and store the details in a variable:

$fw = new-object -com HNetCfg.FwMgr

$profile = $fw.LocalPolicy.CurrentProfile

You can now run PowerShell commands against the profile variable to determine your firewall setup. For example, to display globally open ports:

$profile.GloballyOpenPorts | ft name, port

This may not return anything, as a properly configured firewall on a regular workstation shouldn't have any open ports. But a list of authorized applications is sure to return results, unless you're buried in the depths of the CIA.

Type in this:

$profile.AuthorizedApplications | ? {$_.Enabled} | ft name

And you'll get this in return (that's not a typo, by the way; that's how Skype shows up!).

Name

----

TurboTax Update Manager

TurboTax

SLVoice

Skype. Take a deep breath

iTunes

Bonjour

Microsoft Office Outlook

That's it for today's class. For homework, I urge you to take advantage of the object-oriented nature of PowerShell. With experience and expertise, you can touch many parts of the system and turn this scripting environment into a live programming environment.

Hassell, a frequent Computerworld contributor, specializes in IT topics ranging from networking and security to Windows administration.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about INSMicrosoftNICESkype

Show Comments