35

I'm asking because PowerShell confuses me.

I've been trying to write some deployment scripts using PowerShell and I've been less than enthused by the result. I have a co-worker who loves PowerShell and defends it at every turn. Said co-worker claims PowerShell was never written to be a strong shell, but instead was written to:

a) Allow you to peek and poke at .NET assemblies on the command-line (why is this a reason for PowerShell to exist?)

b) To be hosted in .NET applications for automation, similar to DCOP in KDE and how Gnome is using CORBA.

c) to be treated as ".NET script" rather than as an actual shell (related to b).

I've always felt like Windows was missing a decent way to bang out automation scripts. cmd is too simplistic in many cases, and WSH is too obtuse (although the combination can be used successfully, I'm not a fan). When I first heard about PowerShell I felt like Windows was finally getting a decent shell that would be able to help with automation of many tasks, but recent experiences, and my co-worker, tell me otherwise.

To be clear, I don't take issue with the fact that it's built on .NET, or that it passes objects around rather than text (despite my Unix background :]), and I'm not arguing that PowerShell is useless, but from what I can see, it doesn't solve the problem I was hoping it would solve very well. As soon as you step outside of the .NET/Powershell world, things quit being nice and cozy for you.

So with all that out of the way, what problem did MS solve by creating PowerShell, or is it some political bastard child as I suspect? I've googled and haven't hit upon anything that sufficiently answered that for me, but the more citations the better.

6
  • You are creating a false dichotomy with CMD script vs. Powershell, as right up the middle come JScript and VBScript. Jun 9, 2009 at 0:39
  • This question just screams "flame war". Also very subjective. I imagine this will soon be closed. Jun 9, 2009 at 0:43
  • 1
    Surely the reasons behind MS creating PowerShell have been documented somewhere?
    – Fred
    Jun 9, 2009 at 0:45
  • Did Microsoft document their reasons for creating other products? ;) Jun 9, 2009 at 0:47
  • 3
    See this answer, by PSH's architect: stackoverflow.com/questions/573623/powershell-vs-unix-shells/…
    – Richard
    Jun 9, 2009 at 18:58

3 Answers 3

37

PowerShell was actually built as several things: A mature and extensible automation platform and a modern administration shell.

The former is primarily used for the administration GUIs for Exchange and other server products of recent times. The GUI is just a wrapper around PowerShell which does the heavy lifting behind (kind of like UNIX GUI programs come to be, as a wrapper for a commandline program).

Jeffrey Snover (PowerShell inventor) elaborates a little on how PowerShell was created with which goals and problems it should solve.

In my opinion, PowerShell as a shell is clearly intended as a replacement for cmd (easy to see) and Windows Script Host (Windows Script Host didn't get much attention in recent years, even though it had similar concepts as .NET back in its day [one platform, multiple languages with ActiveScripting], but with .NET Microsoft basically put that to rest and resurrection probably wasn't an option for them).

It unifies most aspects of Windows administration in common concepts and methods you only have to learn once. Also, the power in PowerShell for me stems greatly from it passing around objects which might explain why you get into problems when you step out of the .NET/PowerShell world where you only get a String[] from a command. But for many things you would call an external program for in cmd, there is a cmdlet which will do it for you.

0
10

As a developer, I can tell you I no longer have a bunch of ConsoleApplication42 projects laying around in a folder.

As a developer at a small company where I pretty much do all things IT (DBA, manipulate routers, pull call detail records from the switch, monitor and graph bandwidth for customers, etc...) I can tell you that PowerShell fills a sorely needed gap in Windows and the fact that it's built on .NET provides a seamless upgrade path when the PowerShell pipeline is too slow to handle millions of iterations or a more permanent, strongly typed implementation is needed.

Anyway, I guess the question is why are you switching to PowerShell if you don't have a pressing need? I mean it's good to learn it now since it's basically the new management interface for all things Microsoft. But if that doesn't affect you then don't bother if you don't think you're gaining anything.

EDIT (In response to comments below)

It sounds like you're trying to use the .NET Process class to launch an exe and redirect it's stdout so it can be read by the caller. I agree that is a pain in .NET but fortunately PowerShell does all this for you pretty simply. As for capturing the result and still writing it to the display, that's pretty simple too though the command isn't a very well-known one because it isn't used that often. Here's an example:

# I always find it easier to use aliases for external commands
Set-Alias csc C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe

# Create some source file
Set-Content test.cs @"
class Program {
    static void Main() {
        System.Console.WriteLine("Hello World");
    }
}
"@

# Call CSC.EXE
# the output of csc.exe is written to results.txt and piped
# to the host (or select-string if you prefer)
csc test.cs | Tee-Object -file results.txt

# Check for errors
if ($LASTEXITCODE) { 
    # this is where community extensions would come in
    # handy. powershell 2.0 also has a command to send
    # mail but in 1.0 you can grab one from poshcode.org
}
10
  • I'm involved in a project and it's getting to the point where automated builds, schema updating, deployment, etc needs to be done. I had chosen PowerShell because of my perception that it would be good at the task. My decisions are going to end up being company wide w/i the next year or two and it seems like PowerShell is being pushed heavily by MS. After having experienced some pain with PS, I'm toying with using Ruby or Perl instead (I really don't want to use cygwin).
    – Fred
    Jun 9, 2009 at 1:20
  • Could you use MSBuild for that? I'm not too familiar with MSBuild other than the hand tweaking of csproj files that I've done to hook in ILMerg and what not. But it seems like any scripting language is going to be just as general as the rest. If it's build automation you're after there's plenty of tools that specialize in that.
    – Josh
    Jun 9, 2009 at 1:22
  • Out of curiosity though, what pains have you run into. Perhaps I can help. I'm fairly active in the PowerShell newsgroup and on my blog so I try to help when I can. I found a lot of things frustrating about PowerShell at first but particularly with 2.0 CTP 3 I have pretty much decided that if it ever went away I would sit in my garage with the car running and let the engine purr me to sleep.
    – Josh
    Jun 9, 2009 at 1:24
  • Some of it's nit picky: executing a console app acts differently than a win32 app, and I don't understand why it had to be that way. some of it is less nit picky: How do I call SVN, collect the output so I can mail it in the case of a failure, yet stream it to the current powershell window as a form of feedback, all the while avoiding a deadlock (it happened via the processstartinfo method)? I probably should also have mentioned that I'm using Powershell 1.0 w/o the community extensions :)
    – Fred
    Jun 9, 2009 at 1:55
  • THANK YOU. I looked everywhere, Get-Command, google, etc, looking for that exact bit of functionality, and I somehow missed all of them. I had come to the conclusion that it didn't exist, adding to my confusion about why PowerShell had been created. I had even started looking into writing my own bit of functionality, thank goodness you posted this or I may have found myself on the dailyWTF :)
    – Fred
    Jun 9, 2009 at 5:45
1

IMHO the main advantage seems to be a sensible cut and paste in the command console.

Otherwise I use ActiveState's ActivePerl for scripting on Windows. It is way more powerful than any Windows shell script, and the OLE interface exposes the whole Windows API in an easy-to-use way.

2
  • Right click to cut or paste in cmd its not ctrl+c and ctrl+v but pretty sensible.
    – Copas
    Jun 9, 2009 at 1:23
  • Any links I can use to evaluate that approach James?
    – Fred
    Jun 9, 2009 at 1:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.