Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use PowerShell? [closed]

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.


I'm an admin by trade and just starting in the "Dev" world, but I see that PowerShell can be very useful to developers in a number of ways.

  1. Task automation -> Lee Holmes (a developer on the PowerShell team) posted a proof-of-concept called BgShell, which was basically the PowerShell runtime hosted in a windows forms app that listened for keystrokes and kicked off actions written in PowerShell based on them. It also included some clipboard automation.
  2. Quick access to the .NET framework. You don't have to compile and run something or use another program like Snippet Compiler to test some functionality. Load the assembly into your PowerShell session and interact with it. You get all the discoverability of PowerShell (like Get-Member) to explore your object.
  3. Easy to build domain specific languages. James Kovacs wrote a simple build script language with PowerShell called Psake.
  4. It can provide a scripting language for your application. Since PowerShell is part of the 09 Common Engineering Criteria, a lot of Microsoft applications will have interfaces for PowerShell, and familiarity with the language syntax will grow. You can leverage that knowledge by embedding the PowerShell runtime in your application and providing a scripting interface to your users, choosing what objects they will have access to.
  5. Along the same line, if you need to create a command-line interface for your application, PowerShell provides a large part of the underlying plumbing needed for parsing arguments, and other basic tasks, leaving only the business logic for you to write, and providing a consistent interface with other applications (for example, if you have user accounts that need to be managed (that don't already integrate with Active Directory, an admin could (using the Quest AD Cmdlets and your applications interface) Get-QADUser | New-MyApplicationUser and be done. The Exchange 2007 team did this very well. All the functionality is based on PowerShell cmdlets, the GUI calls the cmdlets and shows the user what is being run, so they can build scripts based off of that.
  6. It's just cool! In about 250 lines (including comments), Rob Foust and Jeff Hicks wrote a network sniffer in PowerShell. Easier to use than WinDump for lightweight troubleshooting.
  7. Community - There is a great community focused around PowerShell, including developers. Find out more at PowerShellCommunity.org.

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.


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


It's never come in use as part of my development role, but every now and then I get a task that I'd write a small program to automate, which I now force myself to do in powershell and it takes less time.

For example, I recently moved hundredsthousands of files from one server to another using xcopy and forgot to set the attribute to preserve the creation dates on the files. Powershell to the rescue. Within ten minutes I had a script written and tested that copied the creation dates over without having to re-copy the files.