Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×

Windows PowerShell in Action 442

jlcopeland writes "For two decades I've hated the command prompt in DOS and Windows. Inconsistencies abound and everything is a special case. The fallback on a Microsoft box has been running a Unix shell under Cygwin or installing Microsoft's own Services for Unix (or its predecessor, Softway's Interix), or by scripting in Perl, but those only get you so far. Having co-written nine years worth of trade rag columns using mostly Perl as the implementation language for the samples, and thinking of every problem that comes across my desk as an excuse to write a little bit of scripting code, I've got some well-formed views about scripting languages and what works and what doesn't. That means I've been eagerly watching the development of PowerShell since it was called Monad. It's got the advantage of being a unified command-line interface and scripting language for Windows, even if it does have a dorky name." Read the rest of Jeffrey's review.
Windows PowerShell in Action
author Bruce Payette
pages 576
publisher Manning
rating 9
reviewer Jeffrey Copeland
ISBN 1932394907
summary Guide to PowerShell, the new Windows scripting language


Bruce Payette's Windows PowerShell in Action is a great overview of PowerShell, aimed at an audience that's got some experience with other scripting languages. Bruce's book is a big improvement over Andy Oakley's earlier book, Monad, which I had been using: it's more complete and it's up-to-date for the first release of PowerShell. It's got great (and sometimes amusing) examples, and feels like the Perl Camel book in flow. When I was reading it in the gym or someplace else away from the keyboard, I kept wanting to run back to the office to try something out. There are also useful "why it works this way" digressions, which provide a lot of context. Since Bruce was on the original development team, wrote most of the commandlets, and was responsible for much of the language design, those digressions are more authoratitive than the directors' commentary tracks on most DVDs.

In outline, the nine chapters in the first part of the book build up as you'd expect: overview and concepts, to data types, to operators, to regular expressions, to syntax, to functions, to interpreting errors. It covers that ground better than many language books that now litter my shelves. The explanations are clear, and the examples are almost all exactly on point. It took me a second reading to realize that my complaints about the regular expression sub-chapter wasn't about the chapter itself, but about some of the implementation decisions; that's an argument about style more than substance, and an observation about me, not about Bruce's writing or PowerShell. The first part of the book is the "mandatory reading," if you will, to get the language down and begin exploring on your own.

The second part is where the real applications are covered. That's the part that you especially want to read sitting next to the keyboard. As you'd expect, the example code is available from the publisher's web site to start you off — look for "Example Code" under "Resources." There's a very good discussion of text processing and how-to-handle XML, complete with some not-obvious warnings about traps to avoid. I've been working very carefully through the really good chapter on using GUIs with PowerShell, "Getting Fancy — .NET and WinForms," and my own proof of concept for that has been rebuilding an old C++ data entry application into a much simpler PowerShell script. As a nice side effect, Bruce's book (and the WinForms chapter in particular) provide a gentle overview to some concepts in the .NET framework, which I hadn't had an opportunity to delve into. The appendix on using PowerShell as a management application will be especially useful to system managers; that was one of the original PoweShell target audiences, and the language achieved that goal very well. The appendix on the language's grammar is really useful, and I keep flipping back to it to check on things.

After Oakley's Monad appeared, there was a long gap before the next PowerShell book appeared. Bruce's book looks to be the first of the post-release wave. If all it had going for it was the authoratative pedigree of the writer, it might be worth it, but it's also well-written, well-organized, and thorough, which I think makes it invaluable as both a learning tool and a reference.


You can purchase Windows PowerShell in Action from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

Windows PowerShell in Action

Comments Filter:
  • Monad (Score:5, Funny)

    by Profane MuthaFucka ( 574406 ) * <busheatskok@gmail.com> on Wednesday May 02, 2007 @02:53PM (#18960757) Homepage Journal
    Named after the designer lost a testicle in a tragic chair throwing accident.
  • PowerShell (Score:2, Interesting)

    by El Lobo ( 994537 )
    is one of the nicest things that came out of Redmond. Ever. My only complain is that it is tight integrated to .NET, but OTOH I believe this is necesary to integrate the always nice C# to it, which is of course a plus.... You can't have the cake and eat it...
    • Yeah. Lets port [ximian.com] to Mono [mono-project.com] ASAP!

      This message piped directly to your Term via stin...
    • PowerShell (Score:3, Interesting)

      I've been using PowerShell for a couple months, and it's definitely better than cmd.exe. However, it's really built more like an interactive scripting environment, with shell features left as an afterthought.

      Only the most basic redirection is implemented. Basically you can use "> file", "2> file", and "2>&1". That's it. You can't create arbitrary fd's and dup them. It's like they didn't realize that the '1' representing stdout and '2' representing stderr actually mean something more general. Oh
  • At this rate... (Score:5, Insightful)

    by pnuema ( 523776 ) on Wednesday May 02, 2007 @03:00PM (#18960875)
    ...in 20 years MS will invent UNIX.
    • Re:At this rate... (Score:4, Informative)

      by JPriest ( 547211 ) on Wednesday May 02, 2007 @03:12PM (#18961013) Homepage
      For those that didn't get it, his comment was a play on the famous:
      "Those who do not understand Unix are condemned to reinvent it, poorly"
    • Re:At this rate... (Score:4, Informative)

      by Shawn is an Asshole ( 845769 ) on Wednesday May 02, 2007 @03:13PM (#18961027)
      You mean again [wikipedia.org]?
    • Re:At this rate... (Score:4, Informative)

      by PhrostyMcByte ( 589271 ) <phrosty@gmail.com> on Wednesday May 02, 2007 @03:19PM (#18961111) Homepage

      Wake me up when *nix gets an object-oriented (rather than text-oriented) shell. Because that is what makes Powershell so unique. Yes, it has plenty of builtin functions to make tasks easier, but the real advantage is that everything you pass between commands is an object.

      You don't have to worry about interpreting text output - you just access whatever data you want directly. Many of the commands are easily chainable into something like "ls | select fullname,length | sort name | format-list | out-printer".

      • Any other requests?

        Actually, I think you can do that with plain old python also, it's just not as "friendly"
      • Re: (Score:2, Insightful)

        by Anonymous Coward
        The shell is a text interface, if I want OOP I can call a different scripting language for every day of the week and then some.

        What's to understand here?
      • by nanosquid ( 1074949 ) on Wednesday May 02, 2007 @03:59PM (#18961825)
        Wake me up when *nix gets an object-oriented (rather than text-oriented) shell.

        "Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea." (Douglas Adams)
    • Re: (Score:2, Redundant)

      "Those who fail to learn the lessons of Unix are doomed to re-invent it. Poorly." -- Forgot who said it.
    • by misleb ( 129952 )
      From what I understand, powershell takes the shell scripting to a whole new level that no *nix does. For example, you can pass objects between applications and other scripts rather than piping text/raw data between them. I'd actually like to give it try some time. Too bad (yea right!) I don't have many Windows servers to put it to the test on.

      -matthew
      • Re:At this rate... (Score:5, Interesting)

        by archen ( 447353 ) on Wednesday May 02, 2007 @03:54PM (#18961733)
        There's actually a lot of other cool things about it as well. I've been using Jscript to do stuff since Win2k (screw that VBscript garbage) but there are obvious limitations at the scripting level, but in the end you're always stuck in cmd.exe

        I was skeptical when I first heard about Monad. I mean it seemed obvious to me that Microsoft just didn't get the point of a "shell" which is supposed to be simple. With a pending install of Exchange 2007, the power shell is required so I figured I'd start to dig into it. I have to say I'm rather impressed.

        First of all, it is actually simple. Not only that, but the syntax is EXTREMELY CONSISTENT. And honestly I cannot stress that enough, because if you think you know part of a command you can usually figure out the verb/noun syntax to use. It also allows shortcut versions of commands so you don't have to type the entire "wordy" version of the command. Aliases are supported too. Another cool feature? You can navigate the registry like the rest of the file heirarchy.

        I'm a big fan of bash, but I must admit that at times it gets old shuffling stuff with awk and cut and so forth. By getting objects you can take what you want out of it, and not worry about the biggest Unix terror - the text output changing. If whatever you're trying to do doesn't support .net objects, you can still pipe text.

        Overall it's pretty awesome technology and I must give MS credit where it's due. Not that I'll be switching any of my FreeBSD servers to Windows because of it, but it makes windows administration orders of magnitude better. Too bad it got dumped in Vista. I've heard it will be included in service pack 1 though.
      • Re: (Score:3, Informative)

        by cduffy ( 652 )
        There are experimental shells for Unix that do that sort of thing; most of us just don't see the point.

        http://geophile.com/osh/index.html [geophile.com]
        http://dispatch.sourceforge.net/ [sourceforge.net]
    • by fm6 ( 162816 )
      Yes, PSH (boy, that name sucks) copies things like pipelines from Unix shells. It also has some features that no Unix shell every thought of, like script signing and support for hierarchical data stores.
  • by PhrostyMcByte ( 589271 ) <phrosty@gmail.com> on Wednesday May 02, 2007 @03:03PM (#18960913) Homepage

    Powershell is very powerful. Much more so than cmd.exe. I don't have significant enough experience with bash to compare the two but I would not be surprised to learn Powershell equals if not beats bash at the shell game. I wouldn't say it is ready to replace any of the scripting languages just yet.

    I have been using it for a while now and the single (semi-major) problem I can find is memory usage. It is a hog at best, and at worst when you are using it semi-heavily it can easily chew up 1GB of memory. That's even with giving the GC something to work with, ie unsetting $vars when you are done with their data.

    • by TBone ( 5692 ) on Wednesday May 02, 2007 @03:11PM (#18960999) Homepage

      I would not be surprised to learn Powershell equals if not beats bash at the shell game. I wouldn't say it is ready to replace any of the scripting languages just yet.

      Unless MS rewrites all of their other commands to accept STDIN/OUT, Monad will never surpass the shells. The power of the shells isnt' their programming flexibility, it's their ability to incorporate all the other UNIX tools and commands via pipes to do what you want.

      I have been using it for a while now and the single (semi-major) problem I can find is memory usage. It is a hog at best, and at worst when you are using it semi-heavily it can easily chew up 1GB of memory. That's even with giving the GC something to work with, ie unsetting $vars when you are done with their data.

      Another reason it will never surpass the shells. They're lightweight, and flexible, and I don't need a Garbage Collector running in the back end to clean up my object allocation.

      • by arevos ( 659374 ) on Wednesday May 02, 2007 @03:58PM (#18961807) Homepage

        Unless MS rewrites all of their other commands to accept STDIN/OUT, Monad will never surpass the shells. The power of the shells isnt' their programming flexibility, it's their ability to incorporate all the other UNIX tools and commands via pipes to do what you want.
        Powershell doesn't use pipes in the same way shells do in unix. Powershell communicates via remote methods and objects, and a lot of the Windows API has been exposed to it, so there's quite a lot of things you can do with it, and a few things that would be a lot more difficult to do with text streams and pipes. It's also quite logically put together, much more so than the standard unix set of command tools. There's not as many third party apps, but most of the basics are there.

        It really fails in two places. Firstly, it's slow. You wouldn't have thought it possible for a command shell to be that slow, but it is. It's so slow it was actually quicker for me to use explorer. It is god-awfully, mind-bogglingly slow.

        The second problem is that it had no easy way of being accessed over a network link, last time I looked at it. So there's no chance of SSHing into a Windows box and administrating it from there, at least not without fiddling with a lot of hacks and workarounds I couldn't get to work.

        The other place where unix shells have an advantage over Powershell is in there interface, as Powershell is currently quite basic in that department. There's limited tab completion and a prompt that can be altered (like PS1 under sh derivitives), but not much over that. Certainly nowhere near my personal favourite, Fish [wikipedia.org].

        Another reason it will never surpass the shells. They're lightweight, and flexible, and I don't need a Garbage Collector running in the back end to clean up my object allocation.
        Why so closed-minded? Powershell has a lot of interesting ideas, and an architecture that's structurally very well organised. Don't dismiss it just because it was made by Microsoft, especially since it sounds as if you haven't even tried it out.
      • I remember one idea that Microsoft was talking about with Monad that actually sounded like a noteworthy innovation-- but I don't know if it still exists. They claimed that any program developed for Vista or Longhorn would have all of their GUI elements automatically scriptable. This seemed potentially useful since I actually have a lot of Windows programs that I have to use for highly repetitive tasks, and I have to use special automation software to accomplish this.

        Microsoft's command-line certainly nee

    • Re: (Score:2, Informative)

      HAHAHAHAHAHAHA. Thanks! I needed a laugh today, and saying that a shell can use up to a gig of memory provided that laugh.

      Let's see on my Mac with OS X, my bash shells, which admittedly aren't being used semi-heavily, are using:

      USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
      nobody 879 0.7 -0.0 27816 856 p3 Ss 11:06AM 0:00.12 bash
      nobody 281 0.0 -0.1 27816 1472 p1 S+ 11:09PM 0:00.17 -bash
      nobody 348 0.0 -0.0 27816 904 p2 S+
      • It looks like you are using ps. ps on Linux can give misleading memory usage reports. Specifically, it counts the entire shared library against every process that loads it. For a program that loads shared libraries, like bash, this results in a significant memory allocation that isn't "real". If one copy of bash was the only program on the system, then bash would use 1 GB. However, 3 copies of bash, don't use 3 GB. They use much less. Additionally, if the same shared libraries used in bash are used i

    • by mangu ( 126918 )
      I don't have significant enough experience with bash to compare the two but I would not be surprised to learn Powershell equals if not beats bash at the shell game.

      Well, you'd be surprised to learn that bash isn't even the tip of the iceberg in Linux. There's also sh, csh, ash, dash, ksh, and I don't know how many other shell options I can't recall just now.

      the single (semi-major) problem I can find is memory usage. It is a hog at best, and at worst when you are using it semi-heavily it can easily chew up

    • It is a hog at best, and at worst when you are using it semi-heavily it can easily chew up 1GB of memory.

      1GB for a command interpreter?

      Christ.

      I mean, just...

      No, I can't come up with words that are strong enough.

      Holy mother of Moore, they've got good crack in Redmond.
  • by Brunellus ( 875635 ) on Wednesday May 02, 2007 @03:05PM (#18960933) Homepage

    I wish they'd kept "monad" as the name. It was a deft tip of the hat to Leibniz's Monadologie, which held that monads were the windowless metaphysical atoms of perception itself.

  • by Angst Badger ( 8636 ) on Wednesday May 02, 2007 @03:06PM (#18960951)
    Inconsistencies abound and everything is a special case.

    You should switch to bash and the GNU tools.

    Oh, wait.
  • by Anonymous Coward on Wednesday May 02, 2007 @03:11PM (#18961007)
    Now all we need is for Sun to develop a Solaris-only shell, Apple to develop a Mac-only shell, and RedHat to develop a Linux-only shell. I hate re-using code because it forces me to solve new problems every day. I'd rather create new value on Mondays only, and then spend the rest of the week re-doing the same work on my other platforms. It gives my mind a chance to rest, and I can drink heavily mid-week and still be able to do my job.

    I sure hope they charge extra for it, make it a resource hog, lock out third-pary extensions, and then discontinue it as soon as I'm dependant on it. I really liked the 1980s and look forward to reliving them.
  • Great Review (Score:5, Interesting)

    by water-and-sewer ( 612923 ) on Wednesday May 02, 2007 @03:14PM (#18961047) Homepage
    I don't use Monad (:s/M/G/g) or intend to, so I don't care much about the book. But what a great review. We get a lot of amateur reviews around here, but this one was particularly well written, clear, and informative. Nice job, homie.
    • Please keep your thoughts regarding your gonads to yourself. We don't want to hear them.

      However, I agree with your review of the review.

  • a Unix shell under Cygwin or [...] but those only get you so far



    What is wrong with Cygwin? How can he bash Cygwin (sorry, no pun intended) without even bothering to say anything about it?
    • What is wrong with Cygwin?

      Cygwin is a hack. It is an add on to Windows, not an integral part of it. On OS X I can pipe output from GIMP straight to Photoshop. Try that on Windows+Cygwin. Try doing anything interesting that involves both Windows applications and CLI applications within Cygwin.

      How can he bash Cygwin (sorry, no pun intended) without even bothering to say anything about it?

      I imagine because it is common knowledge. Cygwin is an attempt to compensate for the lack of a native shell in Windows, but it is a "good enough" work around.

  • by Hoplite3 ( 671379 ) on Wednesday May 02, 2007 @03:30PM (#18961265)
    Windows Powers Hell ?

    I guess I always suspected it was true, but the beastie mascot of BSD made me wonder if there wasn't room for a little UNIX in Hades too.
  • by 3m_w018 ( 1002627 ) on Wednesday May 02, 2007 @03:31PM (#18961275) Homepage
    It's slower than cold molasses up a hill.

    It takes a few seconds for the prompt to appear, and if I run a "dir" operation with both cmd.exe and PS in a directory with hundreds of files, cmd.exe will beat it in seconds.

    I'm not running a slow machine(core duo 2, 1GB of RAM). Is there something that needs to be configured to make it suck less?
    • Re: (Score:3, Informative)

      by Shados ( 741919 )
      Its the typical .NET deal. First time you run something it takes a bit, after that its instant.

      So basically, what makes it suck less, is to use it more.
    • That's not an apples-to-apples comparison, though.
       
      In cmd.exe, "dir" is "dir." It gives you a text listing of the files and subdirectories of your current directory. In PowerShell, "dir" is an alias for "get-childitem," which returns an object array that can either be parsed for display in the console, or passed down the pipeline to another commandlet.
    • It's that slow with a directory with only hundreds of files? I have directories with tens of thousands of files; I wonder how slow it would be with those.
  • What about MKS? (Score:3, Informative)

    by markdj ( 691222 ) on Wednesday May 02, 2007 @03:36PM (#18961331)
    You forgot MKS toolkit which has most if not all of the standard UNIX utilities along with vi, bash, ksh, sh, awk, sed, etc. What more could you want?
    • You forgot MKS toolkit

      ...and from which this book's author, Bruce Payette, (and a number of the Softway folks) are former employees...

  • I've been using this command-line since 1988, back when it was NDOS.COM, and was much better than COMMAND.COM. Later, they broke off from Norton and became JPSoft. When 2K came out, they changed the shell's name from 4DOS to 4NT.

    So, I've been using NDOS/4NT for almost 20 years now. Why would I bother to learn PowerShell when I already have a shell? And I already have cygwin, so I can use unix tools at the command-line just fine?

    It's sad that more people aren't using this great tool.

  • by sootman ( 158191 ) on Wednesday May 02, 2007 @04:04PM (#18961907) Homepage Journal
    For those who haven never seen Monad in action and are quick to bash (ha! get it?) Microsoft's new shell, take a look at these two [msdn.com] videos [msdn.com]. You'll see that it's much more than just bash on Win32. In fact, if it ever catches on, it'll be Unix's turn to play catch-up, because some parts of it are pretty damn amazing. (Note that those are from 2005, but AFAIK, there haven't been substantial changes.)

    The whole point of Monad is that it's not just text, it's objects. So, unlike Unix, where you work with a command and then filter its output (which is just text), the output of Monad, while looking like text, is actually kind of like pointers back to the real thing, so instead of just doing a Unix-style command | filter | filter, you can say "OK, run this command, now of the things it output, go back and tell me this and this about them." Like, "Of these things that are running, tell me which five are using the most CPU time, then tell me the version of each, and how much memory they're using."

    PS: "...even if it does have a dorky name"--which name were you referring to: the one that sounds like 'testicle' or the one that makes me think of the Lottery? :-)
  • by Evilged ( 1096629 ) on Wednesday May 02, 2007 @04:11PM (#18962035)
    I have to manage a Windows domain of a 1000+ users and Powershell (yes crap name) allows me to do stuff that you used to only easily do with expensive third party stuff quickly in a few lines.
    The only other choice to do something similar on Windows is VB script which I find painful at best. It may not be the best Shell ever but at the moment its the best Windows integrated shell with access to .Net, WMI etc. and it beats Active Directory administration through a GUI any day.
    The book is great by the way, and his blog has loads of useful tips too.
    Anybody who is actually going to try it, will find the powershell community extension very useful. G
  • by Brit_in_the_USA ( 936704 ) on Wednesday May 02, 2007 @04:16PM (#18962159)
    I hope this is not a dupe - I certainly was not aware....
    ...PowerShell is avlaibel for MS OS's older than Vista too:

    http://www.microsoft.com/windowsserver2003/technol ogies/management/powershell/download.mspx [microsoft.com]
  • The thing that absolutely drives me nuts about both the Windows Command Prompt and Power Shell is its inability to copy a multiline string to the clipboard as a single line. For example, when the output of a command wraps from one line to the next, trying to copy that output will leave that embedded newline intact.

    Even worse, all copy operations work on a rectangular block of text, not a true start and end point as in a word processor. It makes it useless for copying the output of a program (whether it be a
  • Bean Shell (Score:4, Interesting)

    by hachete ( 473378 ) on Wednesday May 02, 2007 @05:18PM (#18963271) Homepage Journal
    The Bean Shell. That's it. That's what Gonad is trying to copy. Though I forget - who's trying to copy who this week?

    Same problems too as well - memory consumption up the wazoo and slow as hell. Every time you've got to do a "pipe" you need to look at miles and miles of API.
  • by mnmlst ( 599134 ) on Wednesday May 02, 2007 @10:59PM (#18967207) Homepage Journal

    Thanks to some poor choices in my younger days, I have become a full-blown Microserf herding along 250 Windoze servers, half of them in remote locations. If I had it to do all over again, I would have taken the red pill. This may offend the *nix snobs here, but if MS gets really serious about MSH (the way I keep seeing it when running PowerShell), it will be awesome. I haven't seen anybody here mention that it is built-in with Exchange 2007 and when you run through an E2K7 wizard, the last step is the display of the MSH script that will execute once you click the Finish button. It's also just waiting for you to copy and paste that script before clicking the Finish button, so you can expand it and reuse it later.

    My boss is such a Windoze junkie, he pooh-poohs my scripting efforts at every turn. We often spend hours and hours doing repetitive crap in the GUI's because "we don't have time to work out a script now!". I have avoided getting really deep into cmd.exe and VBscript approaches ever since I first read about Monad during the betas as that crap should be passing away. I've been bursting at the seams for some good books to come out.

    Beware a first effort from MS. If they get serious, the third version will be quite good. In the meantime, a wise sage told me to expect third party vendors to jump on this bandwagon and cook up gobs of stuff to leverage the PowerShelll to save Win Sysadmins keyboard time with canned scripts. That would leave me sucking garbage in the MS Matrix with the rest of the Duracells, but fortunately my boss won't spend any money on decent tools, so I will get to hack out the scripts by hand and really learn MSH. Awesome.

    If you're a Win Sysadmin reading this, be sure to check out http://www.sysinternals.com/ [sysinternals.com]Sysinternals and download the Misc utilities package, especially pstools.exe I use them all the time like a telnet session (via RPC) into remote PC's to clear up networking problems on them. netsh.exe then allows me to remove freakin' static WINS and DNS entries in TCP/IP properties, all without disturbing the user. It doesn't take long to learn and it saves gobs of time.

    Now I need to get back to my Linux lessons so I can use some discrete Linux servers on our edge networks, then they can start appearing closer and closer to The Core.

Two can Live as Cheaply as One for Half as Long. -- Howard Kandel

Working...