Functions

Defining Functions

It is easy to define a function in PowerShell, an example is as follows:

Function SimpleIncrease($InValue)
{
  Write-Host "Input was $($InValue)"
  $OutValue = $InValue + 10
  Write-Host "Result will be $($OutValue)"
  return $OutValue
}
Note that you don't need arguments or you can have several and you can optionally specify the type, for example the following is valid:
Function DisplayDirectoryInfo([String]$DirName, [Int]$Depth)
Then if you don't put a return line in then your function returns nothing. As is common with a number of PowerShell things you can do many more advanced things, see Windows PowerShell: Build a Better Function | TechNet Magazine for some details.

Calling Functions

The only thing to remember when calling a function is that you don't need the brackets, otherwise it is easy, as the following example shows:

$Answer = SimpleIncrease 5
Write-Host "The answer was $($Answer)"
This is vital when you have more than one argument to the function, as then using brackets changes the meaning! So for two arguments you must do something like this:
$DirSize = GetDirectorySize $Path $Depth
Write-Host "The $($Path) directory contains $($DirSize) bytes"
Note, that when you call a PowerShell function, the default is that when you have more than one parameter they are space separated.