Powershell? In my toolkit? It’s more likely than you think…

I’m primarily a Linux admin, but I’m a pragmatic admin, so I use what’s best, and it just so happens that Windows is better than Linux for me in some cases (mostly centralized authentication, but there are also some softwares out there that only run on Windows). The point is that I have a heterogeneous network, and I can’t afford to have some of my machine be “second class” citizens.

I’ve also scripted in bash for years now, sometimes to the point of absurdity. I’m hesitant to switch away, just because I have so much braintrust dedicated to bash, but alas, bash doesn’t run natively on Windows without some work, and I’m generally of the opinion that if you have to work that hard, there’s probably a better way.

Since I started out on batch files in DOS days, I was aware of their…uhh…lets see, how shall I put this politely… deficiencies. Actually managing systems with batch files is not my idea of a good time, and so for a long time, I didn’t do it, but I kept hearing rumblings of another language on the horizon from Microsoft that could be used for effectively managing a system…and those noises were referring to Powershell.

A lot of you have tried powershell, and a lot of you like it. I know, because when I said on twitter that I was using it, some people spoke up very vocally and supported the idea, and gave me hints on what to try. On the other hand, some people went the other direction and accused me of “selling out”, which I thought was interesting, considering that I have nothing particularly against Microsoft, at least in terms of stuff I use at work. Anyway, a lot of people were very much shocked and dismayed at my use of powershell, because of the company that produced it. Lets move past that, be grown ups, and say “if it works for what we need, then we’ll use it”, because that is what system administration is about…getting the job done.

I’ve been playing with powershell now for about 6 hours total, and let me assure you, it helps get the job done.

There are some oddities, but overall, speaking as someone who comes from a background in bash and who is lightly conversational in perl and ruby, it’s a very familiar syntax style and methodology. It was obviously written by people who scripted a lot, because plenty of things that used to be hard are now very easy.

I’m probably going to do a “how to get your feet wet with powershell” post or two in the coming days, because I don’t know enough to instruct you beyond that, but let me show you some of the resources I’ve been using, and maybe you can beat me to the punch.

Windows XP or newer
Windows 7 (or Server 2008) are ideal, because they come with PowerShell and PowerShell ISE (the Integrated Scripting Environment, basically an IDE) preinstalled, but you can get Powershell 1.0 on XP and Server 2003, and Powershell 2.0 on Vista *shudder*.

I’ve actually been working through a book, Windows PowerShell 2.0 Administrator’s Pocket Consultant by Microsoft Press. It’s good, if a little slow. I’ve been supplementing it with some TechNet Script Center tasks. If you get a chance, I recommend working through the date/time example to see how powerful the language can be.

So far, I’m very happy with my new scripting language, particularly when I combine it with ultrapowerful addons, like the VMware Virtual Infrastructure PowerCLI, a VMware specific set of commandlets that let you address vCenter and ESX(i) hosts and guests (and clusters) programmatically. Even in the 6 hours I’ve been messing around, I’ve been able to do things that aren’t possible without a lot of hacking in shell scripts.

If you run Windows and you haven’t dug into powershell yet, it’s worth your time. I hope to show you some examples in the near future to kickstart your way.

If you use powershell, drop a comment and share your favorite tip. I’m eager to learn, and I know everyone else is, too!

  • Did you know that there is a VIM plugin for PowerShell? I use it all the freaking time. The obvious google query shows it as the first link.

    PowerShell is quite possibly the best thing to happen to Microsoft system administration since the advent of Active Directory. Sure most of what PowerShell does could probably be done with compiled .NET code, but why compile when you can script? I’ve written monitoring scripts that upload data into ODBC-connected databases, scripts that suck down Windows EventLog data, and one memorable script that called a Perl script (much better native LDAP tools in perl) to parse the output in order to synchronize group memberships with a non-AD LDAP tree. Plus a bunch of Exchange 2007 related scripts that ease my day to day tasks.

    Great stuff.

  • Jeffrey Snover

    Howdy Matt!
    I’m looking forward to hearing the details of what you like and what you dislike and any suggestions you have for how to make it better or more usable by people with your background.


    Jeffrey Snover [MSFT]
    Distinguished Engineer
    Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • Matt,

    I started using powershell 2.0 in 2009 and was initially impressed at how similar it was to other scripting languges (bas, perl), which made learning it a lot easier. It is now the main scripting language that I use on windows servers mainly due to it’s integration with the .net framework and easy to understand syntax. It’s nice to be able to call a .net method when you need it, instead of reinventing the wheel.

  • Jim Wells

    If you get a chance, I recommend looking through Windows Powershell in Action. It’s interesting in how it explains the language and at the same time explains some of the whys behind how it works and how it compares to other languages.

  • I’m no fan of Windows, but I was very impressed with what I read about Powershell. This and their ergonomic keyboard are the two things I like from Microsoft. :-)

  • I am in strong agreement with your viewpoint on using the right tool for the job. On Linux, you don’t manage your systems with PowerShell. On the other hand, on Windows, it’s unarguably the best tool for managing them. Were I administering Linux systems, I would probably take the time to learn Perl or Python in depth.

    Since I’m a PowerShell user, and you asked me for my favorite tip, I’d have to say: get familiar with Get-WmiObject in PowerShell. WMI is a nearly endless array of information about a Windows OS, and also allows you to monitor for certain system events. In fact, I’m writing a PowerShell module right now to assist with that, called PowerEvents (http://powerevents.codeplex.com – it’s not published right now, but due to be released sometime tomorrow). It might be a little complicated for where you’re at, at this point in time, but it’s really cool stuff if you’re doing PowerShell on Windows.

    Trevor Sullivan

  • Also, I second Jim Wells’ recommendation for Windows PowerShell In Action by Bruce Payette. It’s an excellent book, and explains PowerShell syntax and “plumbing” quite well.

    Trevor Sullivan

  • Like you, I just use the best tool for the job. I’m very strong in VBScript (for Admin Scripting, not Webdev), reasonably strong with Perl, (as well as a smattering of other things both compiled and scripted), and can say that Powershell can be a great boon.

    I like that it makes common things easy, and tries to do “the right thing” when something is ambiguous. However, when I’m writing to generate reports, unless one of the default formats does exactly what I want, I find it tedious…

    …but the new job requires that I know Ruby…. (and I’m loving it!)

  • Favorite Tip: Microsoft Technet Script Center Repository. (Search for gwaldo for my couple submissions)

    Second Favorite tip: Get the scriptomatics (free download from Microsoft) and AD Explorer to browse WMI and AD (respectively).

  • I second the recommendation to check out the
    Microsoft Technet Script Center repository. There are a lot of useful scripts in the forums and database.

    I also like the PowerGUI tools and the Quest AD-Commandlets, Powershell 2.0 has AD commands natively, but the Quest tools are so much better.

  • Scott Murphy

    I first ran across powershell when managing virtual center. I was looking for a way to get information on my scattered data stores and found http://www.peetersonline.nl which has a bunch of powershell one-liners. I don’t know how up to date it currently is, but the info on that site saved me days of frustration. Lots of other sites out there on generating automatic documentation for whatever incarnation of VMware you are into.

  • d.f.

    I always liked Henry Spencer’s quote “Those who do not understand Unix are condemned to reinvent it poorly. ”

    I’m not a big fan of Microsoft, mainly due to what I like to refer to as the ‘Windows Tarpit Effect’, but powershell seems to be a decent enough tool (definitely showing off MS’s historical strengths as a programming language vendor.)

    It doesn’t hurt that powershell is just another step in fulfilling Spencer’s famous quote.

  • DLJ

    PowerShell is fantastic for managing Microsoft servers. What is also brilliant is the vendor support it is receiving, for example NetApp have ported almost all of thier API to PowerShell cmdlets. For anyone that has tried to write a script using the NetApp API in powershell, this is pure GOLD! :-)

    I”ll second Sean’s recommendation of PowerGUI and the Quest AD Cmdlets. I use them almost daily. My tips would be to experiment with the pipline (some of the one liners you can create are brilliant) and I’d also recommended the *PSSession cmdlets for remote administration.

    Finally, regarding Bruce Payette’s excellent PowerShell in Action, the second edition “hopefully” will be out soon, which covers PowerShell v2.

  • PShellExec is a free utility that encrypts and executes any data sensitive PowerShell scripts.
    Details are at http://www.screencast.com/t/fsawR7vSur9s