I’ve heard that Bruce Payette, principal designer on the PowerShell team, says 99% of PowerShell scripts start at the command line and end with an enter key.

That is a key way to start out when experimenting (or just getting things done), but can life be made easier?

Let’s say you need square a number (multiply it by itself), it’s an easy no brainer.

No, let’s say I want to square a range of numbers, 6 to 10.

A little more typing and a shift in thinking. Gotta pipe it to the foreach cmdlet and use a scriptblock.

Now, what if you want to divide two numbers and then square it?

So three different needs and at least three different ways to skin the cat. Where’s the consistency?

### There’s Gotta Be a Better Way

There is. Here’s how I prefer to work. No (context) shift in thinking (so it’s faster).

I quickly get results by passing or piping data of any sort.

### The PowerShell Square Function

It’s a straight forward pattern to get this working.

- Create a function
- Add the
*param* keyword
- Add the
**[Parameter(ValueFromPipeline)]** attribute to the parameter
- Add a
*Process* block for your logic (here, it’s just multiplying the parameter by itself)

function sqr {
param(
[Parameter(ValueFromPipeline)]
$p
)

Process{$p*$p}
}

That’s it! Now I have one way to think about how to square a number, a range of number or a value derived by a calculation.

### Let’s do the same to get a Square Root

Using the steps above, I want to wrap the .NET **Sqrt**,** **[Math]::Sqrt($p),** **in the same way to make it easy and consistent.

function sqrt {
param(
[Parameter(ValueFromPipeline)]
$p
)
Process{[Math]::Sqrt($p)}
}

### It’s Triangles All The Way Down

When working with triangles and circles (for example), builders, carpenters, artists, craftsman (to name a few) work with calculating the radius, diameters of circles and sides of triangles all the time.

Let’s say I have a right triangle with a base length of .5 and a hypotenuse of 1. What is the height of the triangle?

I’ll use the *Pythagorean theorem** *to find the height of this triangle*. *

If assume the height to be the radius of a circle, doubling it will give me the diameter of the circle. (**Note**: I can easily create a function to calculate the diameter from two sides).

My intention was not to give a geometry lesson. When you work with these ratios for triangles, circles, squares and rectangles you begin to recognize common ones. Like 1.732. If you square it you know it is 3 and that the square root of 3 is 1.732. Now I know I’m working with a root 3 rectangle (which have interesting properties and is a whole different discussion).

Now I can easily work in the PowerShell space to quickly do these calculations.

### Action

So, with less than a handful of steps, you can quickly and easily wrap up everyday tasks (some you probably re-type over and over) and make them super flexible.

I added these to my PowerShell $Profile and they’re at my fingertips