PowerShell Reference for Azure Functions

This article describes the PowerShell language support in Azure Functions. Azure Functions allows you to write functions in PowerShell. This reference covers language-specific details for developing PowerShell functions.

Note: Azure Functions supports PowerShell 7.2. For more information, see PowerShell 7.2 release notes.

Function Project Structure

A PowerShell function project has the following structure:

my-powershell-function/
├── MyPowerShellFunction/
│   ├── function.ps1
│   └── function.json
├── host.json
└── local.settings.json

function.ps1 Script

The function.ps1 script is where your function logic resides. The script receives input parameters based on the triggers and bindings defined in function.json.

Input Parameters

Your script receives a single parameter, typically named $TriggerMetadata, which contains the input data and metadata for the trigger. The actual data passed to the script depends on the trigger type.

Example: HTTP Trigger

For an HTTP trigger, the $TriggerMetadata object will contain properties like Req (for the incoming request) and Res (for the outgoing response).

param($TriggerMetadata)

$req = $TriggerMetadata.Req
$res = $TriggerMetadata.Res

# Access query parameters
$name = $req.Query.Name

# Access request body
$body = $req.Body | ConvertFrom-Object

# Set the response
$res.StatusCode = 200
$res.Body = "Hello, $name!"

# Return the response object
return $res

function.json Configuration

The function.json file defines the triggers and bindings for your function. It uses a JSON format.

Example: HTTP Trigger Configuration

{
  "scriptFile": "function.ps1",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "TriggerMetadata",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Res"
    }
  ]
}

Common Binding Types

Type Direction Description
httpTrigger in HTTP request trigger.
http out HTTP response binding.
queueTrigger in Azure Queue Storage trigger.
queue out Azure Queue Storage output binding.
blobTrigger in Azure Blob Storage trigger.
blob out Azure Blob Storage output binding.

Managing Dependencies

You can manage PowerShell module dependencies using a requirements.psd1 file. Place this file in the root of your function app project.

# requirements.psd1
@{
    # Add modules here, e.g.:
    # Az = "5.0.0"
    # PSWriteHTML = "1.0.0"
}

When the function app starts, Azure Functions automatically installs these modules.

Runtime Considerations

Important: For production scenarios, it's recommended to explicitly define your module versions in requirements.psd1 to ensure predictable behavior.

Further Reading