How To Create PowerShell Azure Function

In this Azure tutorial, we will discuss how to create a PowerShell Azure Function.

Subscribe to Our YouTube Channel for more videos.

Well Before going to the actual development, you can check out What Is Azure Functions.

Create PowerShell Azure Function Visual Studio Code

Before starting, we should know the Prerequisites for developing the PowerShell Azure Function.

Prerequisites

Azure Functions PowerShell Examples

Now that we have all the Prerequisites needed for the development activity Let’s start the actual development.

  1. The first step is to open Visual Studio Code from your local machine and then click on the Azure Button from the left side. Next, click on the Create New Project button in the Functions Explorer, as shown below.
azure function powershell

2. The next step is to choose the location to save your PowerShell Azure Function project. You can click the Browse button to navigate to the local path on your development machine where you want to save it.

azure function powershell example

3. Now, select PowerShell from the window below. It is very important to choose PowerShell.

azure function app powershell example

4. The next step is to select the trigger as the template. Based on your requirements, you can choose an HTTP trigger that will invoke your Azure Function or another type of trigger.

azure function app powershell

5. In the next step, Provide a unique name for your Azure Function and hit Enter.

Azure Functions PowerShell examples

6. Now the time to choose the Authorization Level as highlighted below

azure functions powershell

7. You can click on the Open in new window option to open the project in a separate window, as shown below

create azure function powershell

8. Now, it will start creating the PowerShell Azure Function. Once it creates the project, you can see the code below is for the run.ps1 file.

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

if ($name) {
    $body = "Hello, $name. This HTTP triggered function executed successfully."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

Run PowerShell Azure Function Locally

Now, Our Azure Function Project is successfully created. The next step is to Test the PowerShell Azure Function Locally. Click on the Run button from the left side, or press the F5 button to run the PowerShell Azure Function Project in Visual Studio Code IDE.

You can see the PowerShell Azure Function Project ran successfully without any issues and provided us with the URL below

MyNewPowerShellAzureFN: [GET,POST] http://localhost:7071/api/MyNewPowerShellAzureFN

azure function run powershell script

Now Open your Favorite browser and paste the URL. You can see below that it is prompting us to append the name value as a query string parameter along with the function URL