Categories

PowerShell Basics: 1 What is PowerShell?

Discussions at the PowerShell Deep Dive and the entries for the Scripting Games showed me that there is tremendous amount of interest in PowerShell but that there are a lot of people that are still unsure of how to get started on want to take PowerShell a bit further than stringing cmdlets together on the command line.

Don’t misunderstand me – the interactive use of PowerShell on the command line is one of its greatest strengths and something I use practically every day. In this series of posts I want to look at first steps in using the PowerShell language. We’ll cover using the pipeline as we go.

I’ve been asked many times what is Powershell and I’ve heard many answers to the question:

  • Interactive and interpreted .NET language
  • .NET at the command line
  • Scripting language
  • Command line admin tool
  • Command line admin tool for AD, Exchange, <insert your technology here>
  • Replacement for cmd.exe
  • Replacement for VBScript
  • etc
  • etc

My answer is that PowerShell is Microsoft’s Automation Engine. The greatest feature is that it can be used interactively and in scripts and you will get the same results. This means you can experiment at the command line, get everything working and then move it straight into a script. The opposite is true – you can run a script so that variables are kept in memory and then experiment to get round a difficult problem.

PowerShell can be thought of as two sets of functionality:

  • cmdlets that supply a way to do something
  • a language that wraps round the cmdlets to help manipulate the results.

In some cases the two can overlap  for instance if you came from a traditional scripting background you might do something like this

001
002
003
004
005
006

$processes = Get-Process 
foreach ($process in $processes
){
 
if ($process.Handles -gt 500
){
  
Write-Output $process
 }
}

 

Use the Get-Process to get a list of processes; test each process to see it uses more than 500 handles and if it does write out the process information.

At this point anyone who has used PowerShell will be shouting “use the pipeline”

We can get exactly the same results like this

Get-Process | Where-object {$_.Handles -gt 500}

 

We pipe the Get-Process results into Where-Object which filters out those processes with more than 500 handles.  We can then take it a stage further and sort the results

Get-Process | Where-object {$_.Handles -gt 500} | sort-object Handles –Descending

 

Adding a sort to our first way of doing things would be more difficult.

 

The first thing to remember about PowerShell is Get the pipeline to do the work for you. The PowerShell team have done an excellent job of supplying utility cmdlets such as sort that can save masses of time and effort compared to trying to write your own.

Next time we’ll look at the utility cmdlets in more depth.

One Response to PowerShell Basics: 1 What is PowerShell?

Leave a Reply