Top 10 Windows 7 Features #9: Native PowerShell 2.0

By Scott M. Fulton, III | Published May 4, 2009, 6:19 PM

Banner: Special series

Microsoft Windows 7 story background (200 px)Ever since the command-line tool code-named Monad escaped by the skin of its fingernails from Microsoft's laboratories in 2006, there has been debate and dispute over whether the company has finally, once and for all, replaced DOS. Since that time, we've seen the arrival of an entirely new generation of Windows users who believe "DOS" is an acronym for "denial of service," and who are baffled as to the reasons why anyone would want to command or control an operating system using text.

It isn't so much that text or command-line syntax is the "old" way of working and that Microsoft Management Console is the "new" way. As Microsoft discovered, to the delight of some in its employ and the dismay of others, using the command line as the fundamental basis for Exchange Server improved its usability and efficiency immensely. The graphical environment simply does not translate well -- or to be fairer, not effectively -- to the task of administration.

As of last October and up through today, at least, Microsoft's often variable plan on PowerShell has been to include it with every SKU of Windows 7 and Windows Server 2008 R2. We will not be surprised if we suddenly discover it isn't included with the Home Basic edition, but its inclusion even there would be an indication that Microsoft is confident about being able to hand over the real-world equivalent of Doctor Who's "sonic screwdriver" to every Windows 7 user in existence.

PowerShell can be used exactly like DOS, as it maintains a vocabulary of "aliases" that recognize DOS commands as alternates, but it is not DOS. Rather, it is a very sophisticated administrative language designed not for programmers or for veteran developers, but instead for admins and system managers who need the ability to communicate a lot of information in as little space as possible.

Looking at just the front end, PowerShell takes the user right back to the TRSDOS era, with a blinking cursor beside a carat. But from there on, the resemblance to the 1970s becomes fleeting and momentary. You do not have to learn the MS-DOS batch file language to create a simple script that enables you to make a common fix. That doesn't mean learning PowerShell isn't a skill in itself -- it is, believe me. But its principles of consistency and economy of verbiage (something I myself may yet aspire to) enable anyone to theoretically craft and deploy a safe and powerful set of free tools that easily substitute for a myriad of commercial anti-malware and system tune-up programs.

Here's an example I created for a tutorial on PowerShell for InformIT last November: Imagine in your mind an MS-DOS batch file whose purpose is to scan the running processes in Windows, find Windows Media Player, and make a log of the amount of available system memory whenever it does find that app. Assuming there were some kind of API for running Windows processes in DOS (and there never was), the amount of conditional statements and print formatting instructions would make such a script enormous.

With PowerShell, I managed the entire concoction in just two instructions:

$table = @{Expression={$_.VM};Label="Memory";width=10},@{Expression={$_.CPU};label="Uptime";width=10}, @{Expression={get-date};Label="Time";width=24}

get-process | where {$_.ProcessName -match "wmplayer"} | format-table $table | out-file "mediaplayer.log" -append

Now, there's a lot going on with these two instructions, thanks to PowerShell's ability to compress a lot of instruction in a little space. The first line, for example, actually creates a little database table in memory, and formats it for the columns needed for this little log file. Each of those columns is given a name, just as though we were building this table for MySQL or SQL Server. The format becomes a kind of template string in memory, which is given the name $table (TRS-80 veterans will remember the old BASIC language where the $ character fell at the end of the string variable name rather than the beginning).

The second instruction is actually in four segments, and each segment passes on its results to the next by means of the pipe | character. Commands that are native to PowerShell are called cmdlets (pronounced "command-lets"), and they all have a verb-noun syntax. There are dozens of cmdlets that start with get, and because the syntax is consistent, it's easier to remember which command does what function. The get-process cmdlet is fairly self-explanatory, in that it generates a list of running processes. If the instruction stopped there, it would generate that list on-screen, but instead the pipeline takes the output to a where function that filters it. During this process, the output of get-process behaves like an object, as in "object-oriented language," so you can refer to the members of the object like properties. This script aligns those properties with the template created for the $table variable.

Next: How far does PowerShell 2.0 go?

1 | 2 | Next Page →

Comments

View comments by with a score of at least

this could help us immensely. this would let us easily set configs, change settings, etc through group policy automatically or waiting until we update our images.

the only downside is we're entirely XP Professional. unless they push PS to XP Pro this wont affect many businesses or schools for years =/

Score: 0

|

It's a mistake to use gray on gray syntax highlighting, needs more contrast.

Score: 0

|

PowerShell is fine for the geeks..for Joe Average, MS need something built atop PowerShell like Automator.

Score: 0

|

Windows 7 RC public download page: http://www.microsoft.com...windows-7/download.aspx

Enjoy :)

Score: 0

|

"they don't pay their workers to use or know powershell"

Really?

Good Windows systems administrators/engineers will know how to perform their tasks from the command line, whether that be with DOS batch, VBScript or PowerShell. If you think admins/engineers touch each PC or server individually, you're crazy. Businesses want their admins/engineers to perform their jobs effectively and efficiently, and that often cannot be done without the use of various scripting technologies.

Score: 3

|

what? that all business's employ only software engineers?

besides a real software engineers and database programmers will use real programming applications to develop customized solutions.

unless the company is in the business of software development, 99% of the workforce of a typical company are not paid to be scripting.

Score: -3

|

I said nothing of software (or database) engineers, not sure where you came up with that.

I assume from your username that you're an IT person, so why do you want to ignore the admins and engineers? A business is made up of all sorts of people, including IT people. That is especially so in large enterprises, where there could be hundreds of IT people supporting many tens of thousands of PCs and servers. Mine has probably somewhere in the vicinity of 80,000 PCs and servers. Since we frequently need to perform various scripting tasks, would it be more efficient for us to have to install PowerShell on every Windows box than for it to already be there, well hidden from the typical user? Wouldn't it be better for us to know it is already there and we can rely on it when necessary?

Score: 0

|

nothing worst than having a bored co worker fiddle with specialty tools to screw everything up.

i'd rather limit developmental softwares and code writing on specialized computers instead of having them on "all" computers for the sake of convenience. besides, a "real" network makes sharing files and programs between hundreds or thousands of pc's a convenience.

the foundation for my original comment, is that powershell is not needed to be supplied with the o.s. because it can be downloaded on an as needed basis from microsoft, like their power toys or the toys for ultimate vista.

Score: -1

|

"consumers that fatten microsofts wallet want blah blah blah...."

Oh man...

Wow.

There are no words... You've reached critical mass in clueless.

Score: 0

|

obviously you are burdened with a great responsibility to ensure your insufferable whine is heard.

perhaps, you simply need your diaper changed, again.

Score: 0

|

Or I just can't resist ridiculing ignorant BS whenever I see it?

Seriously, the level of ignorance in that OP of yours alone is off the charts. Just because *you* can't see a use for it is a failing of your own ability to think, not the ability of any given product or feature to be useful to others.

I love your level of arrogance. "If I can't see a need for it, *obviously* it's useless to *everyone*!" "If it's too kompeekated for me, *obviously* it will confuse *everyone*!"...

Yeah...

Thankfully, not everyone is as narrow-minded, lacking in creativity, or just plain ignorant as you are. :)

Score: 0

|

It's not a decent scripting shell...it's a very good scripting shell! You can do some great things with it without much effort. This was an area MS was REALLY lacking in (any form of a useable scripting shell) for a very long time, and I'm glad they've finally addressed it, and did a very good job imho. I've been using PS1 whenever I need anything scripted...except our login script which I still use KiX for. I haven't used PS2 yet, but one of the features I'm most looking forward to is powershell remoting which was sorely lacking in PS1. It's been in CTP for a while now but I just haven't gotten around to using it yet.

MS has also begun to incorporate powershell into pretty much everything they develop now. Exchange 2007 is fully PS integrated, you can do everything in PS that you'd be able to do through the system manager, but much quicker ofcourse since it is a scripting language...:)

Score: 0

|

Exactly.

It's been too long coming, and it's powerful. Linking it to their Apps, and OSes is an obvious step (well, for most of us...), and other vendors will very likely follow suit from what I've seen in various demonstrations so far.

Can't wait to start messing around with it. (Never really got into PS1, but we're going into a full update cycle in about 8 months and I am hoping Win7 is out by then...the possibilities seem almost endless) Win7 & WS2k8 & PS2.... Heh... Exciting time to be a (mostly) Windows shop. :D

Score: 0

|

PDC 2009: What have we learned this week?

There was the freebie that no one will forget, the heebie-jeebies courtesy of Scott Guthrie, and a teensy bit clearer picture of how this cloud thingie should work.

Live report: Will Google Chrome OS change Linux?

The mysteries of just what Chrome OS is, and how much of an operating system it truly is, may be resolved today.

PDC 2009: Microsoft cares about Web browser performance

The effort to give users of the world's dominant Web browser the impression of quality, is a personal one for the man who leads that battle.

Nokia re-affirms its commitment to Symbian, sort of

Maemo won't necessarily be replacing Symbian in the Nokia N-Series, but that's definitely a place where it will be found.

E-book readers will be in short supply this holiday season

E-readers are hot this year, and a lot of compelling new products have been released, but are there enough electrophoretic displays to go around?

Sony looks to finally open a single storefront for downloads

Sony has had many different download portals for movies, music, e-books, and games, and now it's looking to make a single shop for all of it.

Tuning out the tablet: Time to give the endless speculation a rest

Wide Angle Zoom: Wishing and hoping and thinking and praying....won't put an iTablet on the market.

Five improvements for IT managers in 2010

If businesses are to improve their efficiency for next year, they need to stop and reassess the basic tenets of their job.

AOL's spinoff from Time Warner to shed 2,500 jobs

As AOL moves toward become an independent company again, it will cut nearly a third of its workforce.

Gartner: SMS-based money transfer will be bigger than mobile browsing, search

Gartner issues its predictions for the 10 things our phones will be doing in 2012.

Don't forget to upgrade to Firefox 3.6 beta 3 today

Mozilla has released the latest beta its Firefox 3.6 browser software, just over one week after beta 2.