How do you use PowerShell?

2019-03-08 08:05发布

Windows PowerShell came out last year and got great reviews from many .net bloggers (Hanselman comes to mind). It seemed to be touted as a great new utility that somehow made everything that you would ever do on the command line easier, and integrated with .Net. However, the more I read about it, the more it seems to be a tool that is great for IT professionals, and not much use for developers.

Do you use PowerShell in your dev work? If so, how? Is it worth learning?

Note: After seeing the responses so far, I think it is valid to conclude that PowerShell can be very useful to a .Net developer. However, there is no one answer below that I can label as the answer (so please forgive me for not doing so). I am voting up each answer that I have found helpful.

14条回答
来,给爷笑一个
2楼-- · 2019-03-08 08:40

i use it for multiple process starting . and for some thing that cmd cant do .

查看更多
放荡不羁爱自由
3楼-- · 2019-03-08 08:43

Although I haven't learned how to fully use it yet, I'm a fan because it supports most (if not all) of the Unix commands I'm familiar with for navigating using a command line. If you have *nix experience and are developing on Windows, I would recommend using it for that simply because you'll only have to remember one set of commands.

Take what I say with a grain of salt, however. When it comes to building and compiling, I use the command line functionality.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-08 08:46

You can hate Powershell, and yet it can still be incredibly useful to you. I use it for small but important one-liner types of things or in very minor scripts. It really can't compare to C# so as soon as there is any complexity or significant potential reuse at all I switch to C#. Also, importantly, Powershell is so funky that I WANT to recreate solutions from scratch every time, otherwise I forget the quirks, of which there are many. I have heard other people say this too!

Examples of what it's really good for:

  1. Ad hoc text processing. Occasionally people hand me a large data or SQL file, and they want it manipulated into a different form, sometimes in really complicated ways. This alone has saved me incredible amounts of time. It often involves lots of adhoc one-liners and intermediate files. Now that people know I can do this, they tend to hand such projects off to me. Or in some cases they are so wowed that they learn Powershell for themselves.

  2. When I'm at a customer site and desperately need to automate something, and it's the only tool I can and/or am allowed to get my hands on.

  3. Little scripts to log into web sites and navigate to whatever page I'm interested in or working on. I never manually log into a web site that I'm developing anymore. Trivial, but that's one less annoying repeating brain dead task I need to worry about.

  4. One-liners to copy files and projects around and search and replace.

  5. Little scripts to do builds, if there are any unusual complications involved.

  6. Etc. You are bound to have little quirks in your system, where you need to stop/start a service to fix something else, or whatever.

查看更多
闹够了就滚
5楼-- · 2019-03-08 08:46

I just recently finished version 1 of a project that hosted a PowerShell runtime to execute tasks on remote systems. I disagree wholeheartedly that PowerShell is not a dev's tool.

As a .NET based product, it is a direct analogue to .NET programming. Whole .NET programs can be prototyped in PS, as well as little oneliners to test functionality of methods...etc. That the language architects for PS specifically designed PS's syntax to be close to C#, means there is very little context switch when writing PS scripts and C# code(something I had to do alot recently). You're building on your knowledge already hard won with .NET languages, using PS.

As an automation tool, it is superb. It access's WMI, ADO, ADSI, .NET, COM natively, plus can be spot-welded to work with anything else. This alone makes any Windows automation easier and more powerful. The amount of access to the system is paralled by some other langauges (VBscript, Python) but the ease of use of the language and the benefit of the .NET framework means that some PS scripting is tying together existing code (code reuse) instead of wasting your time writing yet another file zipping routine (for example...)

There are already projects out there that are making build tools (psake comes to mind) to make dev's life easier. Projects like Powershell Community Extensions show the powerful extension capabilities normal every-day devs can achieve. I agree with some of the posters above, I consider anything done to setup build envrionments or work with data for dev tasks, can be done with PS, if not faster/easier/better, but just as well.

James

查看更多
贼婆χ
6楼-- · 2019-03-08 08:48

I use it for IT Pro type stuff a fair bit (for which it's brilliant), but don't think I've used it much for development - occasionally for quickly trying out something like a String.Format. Something I will be doing is to develop powershell commandlets for some of our applications - to allow administrators to perform tasks like user management through powershell, but I don't think this is really what you mean.

查看更多
时光不老,我们不散
7楼-- · 2019-03-08 08:50

It's definitely worth learning. Do you ever convert text? Do you ever manipulate XML? Do you ever automate anything? Simple example. Someone gives you a comma delimited file and you need to pull some fields out of it to import into some other tool/database or to create some list for your program.

If the csv has the field names in the first line of the file, you can do the following in Powershell:

$Data = import-csv "something.csv" 

You can then refer to your data using the field names. If you had a field called SSN then you could say $Data[n].SSN. You could also just pipe the import-csv command to something that processed the data including exporting it as XML/html or anything you want with just a few commands. Powershell is full of stuff like this.

It's a tool that gives you tremendous access to a ton of stuff on your workstation and servers, including the complete .NET library. It's a tool that isn't targeted at a type of person. Maybe it's just that the Admin types have been quick to realize how useful it is. I use it every day.

To actually answer the question asked, I use it to automate things I do everyday. I am trying to make it my single scripting language, replacing the things I did in perl, python, vbscript, .bat/.cmd, and one off little utility/conversion programs that I would create.

查看更多
登录 后发表回答