SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Powershell in a Month – Day 6 The Pipeline

This is part of my Powershell Challenge, to learn more about PowerShell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

Day 6. This is where I found the idea of Powershell getting interested. I used pipes extensively back in my Unix and DOS days, and have heard that these are important in powershell.

The pipeline (|) essentially takes the output of the left command and sends it to the right. The first examples are with exporting data, such as:

Get-Service | Export-CSV services.csv

This command would run the Get-Service command to get a list of services, then send that output to the Export-CSV command, which would create a file, format the data in CSV format, and send all the data from the Get-Service command into the file. Simple, easy and it makes sense. There are also Export-CliXML, -FormatData, etc. commands. On my system I even have an Export-VM  command for Hyper-V. I can start to see possibilities.

This is different from the redirector (>), which is essentially running the pipe to Out-File, a command to create a file. There are other Out-* commands that send data other places, like printers, the screen, etc. Why does it matter that you have these commands? Well, mainly because you might want to alter some parameters, like display width, encoding, etc. Having a real command build allows for parameters, and control, and thus, customization.

Powershell is all about customization, and being able to repeat that process. This chapter gives the basics of controlling your flow out output, including to other commands. For example, one of the things the chapter has you do is

Get-Process -name Notepad | Stop-Process

A fancy way to kill a process. It does warn you not to forget the -name parameter, as that would be bad. I was tempted to try it on a VM, but I know what will happen, so I didn’t bother. For commands that might cause problems, the chapter goes over the -confirm and -whatif parameters, which, respectively, ask you questions to confirm your actions, and show you the action without performing it. The lab for the chapter has you dig into other parameters, like appending to files (noclobber) using the default regional settings (useculture) and changing the delimiter for export. I actually found these lab exercises to be slightly challenging, but also handy. I got to use the help system and proved to myself a bit that I’m learning how to use PoSh.

Overall, no great leaps in knowledge here, but I find myself gaining some confidence that I understand how PoSh works, how to manipulate the environment a bit, and I can see the possibilities of using the pipeline to string commands together and manage more than a simple stream of output data.

The next chapter looks at more involved strings of commands, and I’m looking forward to that one.

Filed under: Blog Tagged: career, powershell, syndicated

The Voice of the DBA

Steve Jones is the editor of SQLServerCentral.com and visits a wide variety of data related topics in his daily editorial. Steve has spent years working as a DBA and general purpose Windows administrator, primarily working with SQL Server since it was ported from Sybase in 1990. You can follow Steve on Twitter at twitter.com/way0utwest


Leave a comment on the original post [voiceofthedba.wordpress.com, opens in a new window]

Loading comments...