TR Dojo: Five advanced PowerShell scripting tricks for Windows admins

February 25, 2011, 7:21am PST | Length: 00:05:33

View Transcript

Bill Detwiler shows you five PowerShell techniques that can put you on the path to becoming a scripting ninja. Once you’ve watched this TR Dojo video, you can find a link to the original TechRepublic article and print the tip from our TR Dojo Blog.

Transcript

>> Bill Detwiler: PowerShell scripts can save you a lot of time on Windows admin tasks but to get the most from them there are a few concepts that you need to know. Well I'm Bill Detwiler during this episode of TR Dojo I'll show you five PowerShell techniques that can put you on the path to becoming a scripting ninja.

music The first PowerShell technique I'm going to show you is called pipelining. Now pipelining is the term for feeding one commands output into another command. Now this allows the second command to act on the input it has received. To pipeline two commands or commandlets simply separate them with the pipe simple. Now here's an example of how it works: Imagine that you want to create a list of processes that are running on a server and then sort that list by process ID number. Now you can get a list of processes by using the get process commandlet but the list won't be sorted. If you pipeline the commandlets output into the sort object ID command, the list will be sorted. Now although pipelining lets you feed one commands output into another command, sometimes it alone won't get the job done. When you pipeline a commands output into another command, that output is used immediately. Now occasionally you may need to store that output for awhile so you can use it or reuse it later. This is where variables come into play. Now it's easy to think of a variable as just a repository for storing a value but in PowerShell a variable can store a commands full output. For example, suppose you want to store the list of processes running on a server as a variable. To do that you could use a line a code like the one shown here. Now in this example the variable is named dollar sign A. If you want to use the variable simply call it by name. For example, typing dollar sign A prints the variables contents on the screen. You can even assign a variable to the final output of multiple commands that have been pipelined together, just surround the commands with parenthesis. For example to sort the running processes by process ID and then assign that output to a variable, you could use a command like the one shown here. The third item on our list is the hash table which is basically a collection of name value pairs, something like an array. By using the at symbol and a pair of curly braces you can turn the contents of a list into a hash table. For example, take the line of code shown here. It creates a variable named dollar sign procks that contains multiple lines of text and you can also use the at symbol when the variable is used, to ensure that its treated as a hash table, rather than a single value. For instance this line of code will run the get process commandlet against the variable I defined just a moment ago. In doing so, Windows will display all the processes used by Windows Explorer and SBC host. Now notice how the at symbol is being used in the front of the variable name instead of the dollar sign that we usually use. Now you can do a lot with hash tables and you can manipulate their contents in a variety of ways. I'll link to a Microsoft tech net article that shows you some more hash table tricks in the TR Dojo blog. Next on our list is the split operator which splits a text string based on a character you designate. For example, suppose that you want to break a sentence into an array consisting of each individual word in the sentence. You can do so by using the command like the one shown here and the results would look something like this: Just as split can split a text string into multiple pieces, the joint operator can combine multiple blocks of text into one. For example, this line of code will create a text string consisting of my first name and last name. The space between the quotation marks at the end of the command tells Windows to insert a space between the two text strings. Well that does it for this episode. For even more PowerShell tips check out Brian Posey's article: Ten fundamental Concepts for PowerShell Scripting on which this video is based. I'll link to it on the TR Dojo blog. And as always, for more teachings on your path to becoming an IT ninja, visit trdojo.techrepublic.com. Sign up for our twice weekly email newsletter, or you can follow me on Twitter. Thanks for visiting the TR Dojo.

pause in speaking Well I'm Bill Detwiler and during ok. Sometimes pilot pipelining pipelining. And as always for more teachings on your path to being. I'll show you five PowerShell techniques that you can put on the path to becoming that you can put that can put you you can put can put you can put you backwards and forwards today.

==== Transcribed by Automatic Sync Technologies ====