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.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Jonathan Hassell

Computerworld
Show Comments

Cool Tech

Toys for Boys

Family Friendly

Stocking Stuffer

SmartLens - Clip on Phone Camera Lens Set of 3

Learn more >

Christmas Gift Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Cathy Giles

Brother MFC-L8900CDW

The Brother MFC-L8900CDW is an absolute stand out. I struggle to fault it.

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?