Ezine 180 Three Commands to Get Started with PowerShellEzine 180 Three Commands to Get Started with PowerShellEach Ezine, has two purposes, firstly, to provide help when you have a problem understanding one of PowerShell's building-blocks, secondly, to provide interesting examples for those running PowerShell through its paces.
This Week's Secret - 3 Ways to Learn PowerShellThere are two main ways that people learn a programming language. Some simply copy and paste other people's scripts, while others undertake a course of structured programming methodology. I offer, and indeed specialize in a third way - project based learning. The idea is to learn PowerShell through a series of modules, each of which tackles a real-life task. One reason that PowerShell is suited to this learning approach is that its easy-to-understand commands deliver a big punch in just a few words. To take an example type, (or copy and paste!) get-Process. If you warm to the theme of project based learning, then what tends to happen is that you learn all about a narrow range of PowerShell structures needed to complete that particular task. The benefit is that when you tackle your second project there will be a carry-over of techniques that you learnt from your previous project(s). The secret of learning PowerShell quickly is to choose projects that are worthwhile, and here is where I can help with a free bank of material on my website. My mission is to provide lots of short examples that are simple to modify, yet have detailed explanations of what PowerShell is doing. This Week's MissionThe main purpose of this ezine is to explain that you need to know only 3 commands or techniques to get started with PowerShell. To stay true to my principles, this week I have also included a real-life mission to investigate the operating system's processes. Let us imagine that you suspect malware has invaded your operating system, and you need a list of processes so that you can eliminate them, else research their name on the internet as being a possible virus. Pre-RequisitesBefore you test my short scripts you need to download and install PowerShell and .Net Framework. Next you launch the executable 'PowerShell', now you are ready to type (or copy and paste) my short scripts. PowerShell's Three Learning ToolsOne way of looking at these three techniques is that these are the ONLY commands you need to get started with PowerShell. Furthermore, even professionals use these commands if they get stuck.
get-Help The secret of using get-Help is that precedes the cmdlet that you are investigating. I also have a tip: remember to include the -full parameter. Here are two examples: # PowerShell example to list demonstrate get-Help get-Help get-Process # (Not get-Process get-Help) get-Help get-Process -full get-Command The most obvious way to modify or filter the output is simply to add the first letter followed by the wildcard * symbol. # PowerShell example to list demonstrate get-Command get-command s* Other parameters which fine tune the list of cmdlets are -verb and -noun for example: # PowerShell example to list demonstrate get-Command -verb get-command -verb set # or get-Command -noun service get-Member (alias gm) The benefit of get-Member is that it lists the properties and methods of the cmdlet under investigation. If you take get-Process as an example, without appending get-Member you may not be aware of properties such as company, PriorityClass, StartTime. The trick with get-Member is to precede it with a pipe | thus: # PowerShell example to list demonstrate get-Member get-Process | get-Member # (And not get-Process get-Member, where we forgot the pipe) Tip. Employ the parameter -memberType, for example to filter only the properties try: get-Process | get-Member -memberType property From the results of get-Member you can design your own format for the output thus: get-Process | format-Table name, Company, PriorityClass, StartTime -auto Real-life task - List ProcessesOne scenario for using this script is to investigate processes that could be viruses or malware. Once you have a list, you could 'Google' names that you don't recognise. Now I know you could achieve this via the task manager, but the benefit of PowerShell is flexibility and customization. For example, with a script you can group the processes by Company, and produce a printout. # PowerShell example to list processes in order of Company name get-Process | get-Member -memberType property get-Process | sort Company | format-Table name, Company, PriorityClass -auto I apologise for jumping ahead and introducing the 'sort' command along with another | (pipe), but with project based learning you often have to make such leaps of faith. For more information try get-Help sort-Object -full. One last tip, if you were serious about this task you could send the list to the printer by simply appending the | (pipe) and the command out-Printer; trust me PowerShell will find your default printer. Thus: get-Process | sort Company | format-Table name, Company, PriorityClass -auto | out-Printer Summary of Learning PowerShellThere are many schools of thought about how to learn a scripting language. My chosen method for PowerShell is to learn via a project based approach. What I do is break a project into small units, get each unit working, then bolt the parts together to make a real-life PowerShell script. My message in this ezine is that you only need 3 commands to get started with PowerShell, from these 3 building blocks you can build all manner of useful scripts, the 3 key cmdlets are: get-Help, get-Command and get-Member. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever See more Microsoft PowerShell tutorials• Windows PowerShell Home • Introduction • Cmdlets • Exchange 2007 • Profile.ps1 • $_.Pipeline If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit. *
| |||||