Reputation: 2353
I'm a competent C# programmer, and new to PowerShell. What is it good for? Is it more of a programmer's tool or administrator's?
Please share your experience. When would it be easier to write a script using .NET assemblies than a C# tool? What real, production tasks do you use it for?
Maybe the question should be: What is it good for compared to C#, not batch?
Upvotes: 12
Views: 10212
Reputation: 13229
I think it is better than C# for small things that might require changes frequently.
Mostly I use it for parts of my build and automation setup. The reason I prefer PowerShell, is that my scripts almost always need some DLL file from Visual Studio.
Now, if it is a C# application, when a new version of Visual Studio comes out I need to recompile using the new references and then publish it out to everywhere it needs to be.
However, if it is PowerShell, I can simply change the path of the DLL file in the script and everything is fine. Also, I find it much easier to work with for file parsing and other Perl-ish type work.
Upvotes: 2
Reputation: 39697
I have created both pure PowerShell commandlets and created some in C#. Typically the ones I've done in C# are more complex, and I find it easier to construct several files with classes, etc. in order to maintain the code in a better fashion.
It's also easier to create tests for C# projects compared to PowerShell, as C# is a first-class citizen in Visual Studio, and PowerShell is not.
The advantage of doing it all in PowerShell is that anyone can edit your scripts.
Upvotes: 2
Reputation: 4433
It's slightly more of a system administrator's tool, but only slightly.
The places I'd choose PowerShell over, say, C# are:
We used it to automate our overnight acceptance tests, compile the results, update a webpage with the latest and e-mail the teams concerned.
Note that NuGet uses PowerShell to automate the adding / removing of packages to your solution. (And in the process exposes a PowerShell console for you to automate Visual Studio yourself.)
Plus the ability to evolve a script interactively, rather than the write → debug → fix cycle you get in a compiled language can make it quick to knock up simple scripts.
Upvotes: 2
Reputation: 20044
For writing a C# tool you typically need to set up a Visual Studio project (or a project in another IDE). If you doing this "manually", you need at least a build script to run the C# compiler. If for a specific task this seems to be too much overhead, and you need just a simple one-file-source-and-program-is-all-in-one solution, a PowerShell script may be the better alternative.
Some possible reasons:
Upvotes: 6
Reputation: 44605
PowerShell is a powerful tool you can use to accomplish several and different tasks which would otherwise require a user using the mouse... (not only).
For example, the latest versions of Microsoft server products like SQL Server, Exchange, SharePoint and so on expose a rich set of APIs that could easily be used with PowerShell, in this light, with PowerShell you can for example:
All things which you could not do before simply from a batch file.
Upvotes: 2
Reputation: 201612
I'm a C# developer and have been using PowerShell since the beta days when it was still called Monad. I also did a fair amount of development on UNIX including automation/scripting with Korn Shell. For me, PowerShell was a godsend because I was growing tired of Korn Shell's little impedance mismatchs with Windows. For example specifying a network share path was particularly gross "\\\\\\server\\\\share"
IIRC. It was a bit of guessing game as to how many times you escaped a backslash depending on how many times the string gets evaluated.
I use PowerShell for a lot of automation tasks such as:
To replace custom command line utilities:
You can use PowerShell as a great way to replace all those little command line utilities you used to write. Think about this. PowerShell is a reasonably powerful script language that gives you access to most of the .NET Framework. It particularly excels at parameter parsing i.e. it has a built-in parameter parsing engine that gives you: named parmeters, positional parameters, optional parameters, switch parameters, pipeline bound parameters, parameter validation and more. Now consider how much code in your typical command line utility is dedicated to parameter parsing vs the actual functionality. I've almost stopped writing command line utilties (unless they're particularly complicated - then you can't beat the VS debugger). And I let PowerShell handle all the parameter parsing for me. And with PowerShell 2.0 it is extremely easy to add documentation/usage to your utility by just decorating your script with some appropriately formatted comments.
I also use PowerShell as a .NET REPL:
"{0,20:F1}" -f 41.22
.You can also take advantage easily host the PowerShell engine within your own C# application. This comes in handy if you provide features to your end users that you would like to make command line scriptable. You can write those features as a PowerShell cmdlet (in C#). Those cmdlets can then be used directly from the command line and if you host PowerShell in your GUI app, you can then access that same set of code from there e.g.:
private bool VerifyPowerShellScriptSignature(string path)
{
using (var runspaceInvoker = new RunspaceInvoke())
{
Collection<PSObject> results =
runspaceInvoker.Invoke("Get-AuthenticodeSignature " + path);
Signature signature = results[0].BaseObject as Signature;
return signature == null ? false :
(signature.Status == SignatureStatus.Valid);
}
}
Upvotes: 7
Reputation: 97671
When it would be easier to write a script using .NET assemblies than a C# tool?
Ah see, that's just it. PowerShell bridges the divide between batch files and the .NET world. PowerShell answers the question, "What if you were to write a new command interpreter that had all of the power of .NET, was totally dynamic, and had pretty much most of the things that people want in a command shell?"
So when do you use PowerShell over C#? It's a lot more convenient for small apps that integrate or orchestrate other apps -- it's more convenient because you don't have to compile it, AND it already has a standard way of passing data around to other scripts and scriptlets, which is easy for people (who know PowerShell) to understand.
Upvotes: 8