Prof. Powershell

Get-Modular

PowerShell can handle functions, but be sure to keep things simple by modularizing code.

If you've been using PowerShell for a while, you've likely seen functions used in scripts and maybe even tried them yourself. PowerShell does not use subroutines. That's a good thing.

Instead of writing monolithic scripts, I'm a big proponent of modularizing code. Create single-task functions that you can test individually. Then combine them as needed in larger scripts, or load them as part of your PowerShell profile and turn them into tools.

The simplest way to create a function is to use the Function keyword:

Function Do-Something {
#run some code here
}

Once the function is defined you can call it by simply using the function name:

PS C:\> do-something

Whatever code you have between the curly braces is executed. Your function can return information in two ways (technically there are more, but these are what I recommend).

Use the Write-Host cmdlet to write messages directly to the console. These messages don't go through the pipeline and I often use them to let the user know what is going on.

Take advantage of the -foregroundcolor parameter so that you can distinguish output from Write-Host with the other approach, Write-Output, or its alias, Write. Write Output sends objects to the pipeline which is important because often you want your function to participate in the pipeline:

Function Do-Something {
#run some code here
Write-host "Starting update process" -foregroundcolor Green
$procs=Get-wmiobject win32_process
Write $procs
}

Now my function displays a message about what it is doing and writes the WMI object to the pipeline:

PS C:\> do-something | sort workingsetsize -descending | Select Name,workingsetsize | select -first 5

When I run this, I'll see my message in green and the sorted and filtered output. Don't focus on what the function is doing, it's not very practical. I'm only using it as an illustration.

A few final words on functions. First, unlike languages like VBScript, a function must be defined in a script or in the shell before it can be called. You can nest functions within functions to keep all the "functionality" completely self-contained.

While there's no requirement, I encourage people to name your functions like cmdlets with the verb-noun convention. Next time, I'll show you how to pass parameters to your functions.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular