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

Essentials

Mobile

Exec

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Christopher Low

Brother RJ-4230B

This small mobile printer is exactly what I need for invoicing and other jobs such as sending fellow tradesman details or step-by-step instructions that I can easily print off from my phone or the Web.

Aysha Strobbe

Microsoft Office 365/HP Spectre x360

Microsoft Office continues to make a student’s life that little bit easier by offering reliable, easy to use, time-saving functionality, while continuing to develop new features that further enhance what is already a formidable collection of applications

Michael Hargreaves

Microsoft Office 365/Dell XPS 15 2-in-1

I’d recommend a Dell XPS 15 2-in-1 and the new Windows 10 to anyone who needs to get serious work done (before you kick back on your couch with your favourite Netflix show.)

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.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?