Ezine 180 Three Commands to Get Started with PowerShell

Ezine 180 Three Commands to Get Started with PowerShell

Each 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 PowerShell

There 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 Mission

The 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-Requisites

Before 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 Tools

One 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.

  1. get-Help  (Also plain help, get is assumed.)
  2. get-Command
  3. get-Member (alias gm)

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
# Author: Guy Thomas
# Version 1.2 January 2009 tested on PowerShell v 1.0

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
# Author: Guy Thomas
# Version 1.2 January 2009 tested on PowerShell v 1.0

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
# Author: Guy Thomas
# Version 1.2 January 2009 tested on PowerShell v 1.0

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
# Author: Guy Thomas
# Version 1.2 January 2009 tested on PowerShell v 1.0

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

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Real-life task – List Processes

One 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
# Author: Guy Thomas
# Version 1.2 January 2009 tested on PowerShell v 1.0

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 PowerShell

There 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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

If you like this page then please share it with your friends

 


See more Windows PowerShell tutorials

PShell Home   • Introduction   • Dreams   • 3 Key Commands   • PowerShell Help About   • Get-Help

PowerShell v 3.0   • Set-ExecutionPolicy   • Get-Command   • Cmdlet scripts   • Import-Module

PowerShell Version Check   • Backtick   • PowerShell examples   • PowerShell ISE   • Get-Member

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.