Subscribe to Windows IT Pro
April 14, 2009 12:00 AM

Create Your Own PowerShell Functions

Lesson 5 in the PowerShell 201 series explores how to create, call, and use functions
Windows IT Pro
InstantDoc ID #101610
Rating: (3)

You can also provide some values and not others when calling a function. For example, the following command includes a value for the first parameter ($dir) but not the second parameter ($minSize):

FileSize4 C:\Windows

When the function runs, it'll use C:\Windows for $dir and the default value for $minSize. Thus, the result set will list files larger than 1,000,000 bytes in the C:\Windows directory.

When you specify a parameter value in a function call that's not in the same order as the parameters defined in the function definition, you must include the parameter's name. For example, the following command specifies only the $minSize parameter:

FileSize4 -minSize 500000

The function will use the default value for $dir and the 500000 value for $minSize, so the result set will list files larger than 500,000 bytes in the C:\Windows\System32 directory. If you were to provide only the $minSize value without the parameter name, PowerShell would assume that the value is meant for the $dir parameter because $dir is the first parameter defined in the function. For that reason, you must include the parameter name.

Specifying Parameter Types

In addition to assigning a default value to a parameter, you can strongly type the value by casting the variable. To do so, simply precede the parameter name with the data type name (or its alias) within brackets, as in

function FileSize5
  ([string] $dir="C:\Windows",
  [int] $minSize=1000000)
{
  dir $dir |
  where {$_.Length -gt $minSize}
}

Now $dir is defined with the String data type, and $minSize is defined with the Int32 data type. If you try to enter a value with the wrong type, you'll receive an error. For example, the following command attempts to use a string as an argument for $minSize, which is configured as an integer:

FileSize5 -minSize file

As Figure 5 shows, the command will generate an error because PowerShell cannot convert file to an Int32 value.

Figure 5: Strongly typing parameters in a function

Working with Functions

Up to this point, the sample function calls have called the function directly, and the functions' results were returned to the console. However, functions are particularly useful when used in conjunction with other elements in PowerShell scripts. For example, you can use a function to assign a value or a collection to a variable. For example, the code

$files = FileSize5 C:\Windows 500000
foreach ($file in $files)
{
  $file.Name + " is " +
    $file.Length + " bytes."
}

uses the FileSize5 function to retrieve a list of files and assign that list to the $files variable. That variable is then used in a foreach loop to return each file's name and size, as shown in Figure 6.

Figure 6: Using a function to initiate a variable

In addition to using functions to define variable values, you can use functions directly in a pipeline, along with other commands. For example, the following pipeline begins by calling the FileSize5 function:

FileSize5 C:\Windows 500000 |
foreach {$_.name + " is " +
  $_.length + " bytes."} 

The function's results are then piped to the ForEach-Object cmdlet (referenced by the foreach alias), which generates information about each file returned by the function, as Figure 7 shows.

Figure 7: Using a function in the pipeline

Moving Forward

Functions are extremely useful when working with PowerShell scripts that perform the same tasks repeatedly. You can make your functions as simple or as complex as necessary. However, as I mentioned previously, the functions you create within a session are available only during that session. In the next lesson, I'll explain how to persist those functions so they're available whenever you need to call them.

Related Content:

ARTICLE TOOLS

Comments
  • Karen
    3 years ago
    Apr 21, 2009

    There isn't any code to download for this article. I'm not sure why the Download the Code Here button is showing. (It shouldn't be.) I'm having the web team look into the problem.

    Sorry for any confusion caused by this glitch!

    Karen Bemowski, senior editor, Windows IT Pro

  • David
    3 years ago
    Apr 21, 2009

    Code download is missing...404 error

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.