Skip to content

How to Define Functions

A function is a named block of code that performs a specific task. Functions let you organize logic, avoid repetition, and build scripts that are easier to read and maintain. In PowerShell, a function is defined using the function keyword followed by a name and a script block.


1. Basic Function Syntax

The general structure of a function is:

function Name {
    <code>
}
  • Name is the identifier you use to call the function.
  • The code inside {} runs when the function is invoked.

Example: a simple function

function Say-Hello {
    "Hello from the function."
}

Say-Hello

Explanation:

  • Say-Hello is the function name.
  • When you call Say-Hello, PowerShell executes the code inside the braces.
  • The output is: Hello from the function.

2. Functions with Parameters

Functions can accept input values using the param() block.

Syntax:

function Name {
    param(<parameters>)
    <code>
}

Example: greeting a user

function Greet {
    param($name)
    "Hello, $name."
}

Greet "Alice"

Explanation:

  • The function defines one parameter: $name.
  • When calling Greet "Alice", the value "Alice" is assigned to $name.
  • The function prints: Hello, Alice.

Multiple parameters

function Add-Numbers {
    param($a, $b)
    $a + $b
}

Add-Numbers 5 7

This prints 12.


3. Typed Parameters

You can enforce parameter types using type accelerators:

function Multiply {
    param([int]$x, [int]$y)
    $x * $y
}

Multiply 3 4

Explanation:

  • Both parameters must be integers.
  • PowerShell converts compatible values automatically.
  • The function prints 12.

If you pass incompatible values, PowerShell throws an error.


4. Returning Values

PowerShell functions return anything they output.

There is no return keyword required, although you can use it.

Implicit return

function Square {
    param($n)
    $n * $n
}

Square 5

Output: 25.

Explicit return

function Square {
    param($n)
    return ($n * $n)
}

Both versions behave the same.


5. Functions with Script Blocks

Functions can accept script blocks as parameters, allowing dynamic behavior.

Example: running a script block

function Invoke-Block {
    param([scriptblock]$code)
    & $code
}

Invoke-Block { "Inside the block" }

Explanation:

  • The function receives a script block.
  • & $code executes it.
  • Output: Inside the block.

6. Functions and Scope

Functions create their own local scope.

Variables inside a function do not affect variables outside it unless explicitly scoped.

Example: local scope

$x = 10

function Test {
    $x = 99
}

Test
$x

Output: 10.

Explanation:

  • $x = 99 inside the function creates a local $x.
  • The outer $x remains unchanged.

To modify the outer variable intentionally:

function Test {
    $script:x = 99
}

7. Why Functions Matter

Functions allow you to:

  • Encapsulate logic
  • Reuse code
  • Reduce duplication
  • Improve readability
  • Build modular scripts
  • Prepare code for modules and advanced functions

They are the foundation for writing maintainable PowerShell scripts.