---
title: "Creating an Azure Function to Run PowerShell in the Cloud"
description: "Learn how to create an HTTP-triggered Azure Function that runs PowerShell code. We'll walk through setting up the prerequisites, creating the function locally, and deploying it to Azure."
canonical: "https://adamtheautomator.com/azure-function-powershell-cloud/"
---

# Creating an Azure Function to Run PowerShell in the Cloud

> Learn how to create an HTTP-triggered Azure Function that runs PowerShell code. We'll walk through setting up the prerequisites, creating the function locally, and deploying it to Azure.

Source: https://adamtheautomator.com/azure-function-powershell-cloud/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Creating an Azure Function to Run PowerShell in the Cloud](https://adamtheautomator.com/wp-content/uploads/2024/10/55246dc5-5410-42f1-bee8-f2fd699ceac2.webp)

# Creating an Azure Function to Run PowerShell in the Cloud

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)24 October 20245 min. read

Categories: [Cloud](/category/cloud/)

Tags:[Azure](/tag/azure/)[Azure Functions](/tag/azure-functions/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Setting Up the Prerequisites](#setting-up-the-prerequisites)
*   [Creating the Function Locally](#creating-the-function-locally)
*   [Publishing the Azure Function](#publishing-the-azure-function)
*   [Conclusion](#conclusion)

Azure Functions provides a powerful way to run code in the cloud without managing servers. Let’s create an Azure Function that runs PowerShell—perfect for automating tasks or building serverless applications.

## Setting Up the Prerequisites

First, we need to set up some Azure resources. We’ll use PowerShell to create these:

1\. Create a resource group:

```
New-AzResourceGroup -Name "PowerShellAzFunctionsDemo" -Location "East US"
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-11-1024x214.png)

2\. Create a storage account (required for Azure Functions):

```
New-AzStorageAccount -ResourceGroupName "PowerShellAzFunctionsDemo" -Name "psazfunctionsdemostorage" -Location "East US" -SkuName "Standard_LRS"
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-12-1024x147.png)

Every Function App needs a storage account to store function code and other function-related content. We’re using **Standard\_LRS** (Locally Redundant Storage) because it’s cost-effective for demo purposes. In a production environment, you might consider more redundant options.

3\. Create the Function App:

```
New-AzFunctionApp -Name "PowerShellFunctionsDemo" -ResourceGroupName "PowerShellAzFunctionsDemo" -StorageAccountName "psazfunctionsdemostorage" -Runtime "PowerShell" -RuntimeVersion "7.2" -OSType "Linux" -Location "East US"
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-13-1024x462.png)

This is where the magic happens. We’re creating a Function App, which is the container that will host our functions. We’re specifying PowerShell 7.2 as our runtime to ensure our scripts run on PowerShell 7.2. We’re using Linux as our OS type because it’s generally more cost-effective and faster than Windows.

The function app contains all the functions you create.

## Creating the Function Locally

Now that we have our Azure resources set up let’s create the function locally:

1\. Install [Azure Functions Core Tools](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local) (if not already installed).

2\. Create a new function project:

```
func init PowerShellFunctionProject --powershell
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-14-1024x251.png)

3\. Let’s see what it created.

```
gci C:\PowerShellFunctionProject
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-15-1024x378.png)

*   **Host.json** is a global configuration file that affects all functions within an app. It configures things like logging, function timeout, bindings, and triggers across functions.
*   The **local.settings.json** file is a configuration file for local development. You’ll typically work on your function code here locally and then publish it once complete.
*   **profile.ps1** – This is a PowerShell script that runs when your function app starts or a new runspace is created. It’s used for global initialization tasks such as setting up authentication, importing modules, and defining variables available to all app functions.
*   **requirements.psd1** – This PowerShell manifest file s lists PowerShell module dependencies for your function app. It specifies which modules and versions should be automatically installed, ensuring all necessary tools are available for your functions to run properly.

But no PowerShell scripts to invoke. That’s up to us to create.

4\. Create a new function within the project. To create that, I need to create a subfolder inside of the project folder, which will be called the name of the function.

```
mkdir C:\PowerShellFunctionProject\PowerShellFunction
```

5\. Create the `run.ps1` script:

```
using namespace System.Net

param($Request, $TriggerMetadata)

$name = $Request.Query.Name

$body = "Hello, $name. This HTTP triggered function executed successfully."

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})
```

This is the heart of our function – the actual PowerShell code that will run. This is a simple example that demonstrates input and output. When the function is triggered via HTTP (since we set that up in the function JSON file), the HTTP request will be represented via the Request parameter. The Request parameter will contain information like the HTTP verb used, such as GET or POST, the query, and the payload or body of the HTTP request for POST requests.

The script will read the Name HTTP query parameter, assign it the name variable, and then use it in a simple string.

Finally, the function will output information via HTTP, so we need to apply an output binding. The output binding will “bind” the PowerShell script output stream to the function to send back via an HTTP response. To convert the PowerShell output to an HTTP response that the function understands, we must create an `HttpResponseContext` object with two properties:

1.  `StatusCode`: Set to HTTP 200 OK, indicating a successful request.
2.  `Body`: The greeting message we constructed earlier.

This structure allows Azure Functions to properly format the HTTP response that will be returned to the client.

5\. Now, let’s create the _function.json_ file.

```
{
  "bindings": [
    {
      "name": "Request",
      "methods": [
        "get",
        "post"
      ],
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "type": "http",
      "name": "Response",
      "direction": "out"
    }
  ]
}
```

This _function.json_ file is crucial. It defines the bindings for our function. Bindings specify how it’s triggered and how it outputs data.

*   We’re setting up an HTTP trigger that responds to GET and POST requests
*   This function will use anonymous authentication so we can query the URL it will be exposed to without providing any credentials.

The function will return an HTTP response and be called the `$Response` variable within the PowerShell script we’ll run.

This needs to be saved in the same folder as the `run.ps1` script.

6\. Now, navigate to the function folder first.

```
cd C:\PowerShellFunctionProject
```

7\. And see if the function can start.

```
func start
```

> > > **_When developing Azure Functions, you should first locally and then publish it when you’re finished. You can run a function locally by running `func start`._**
> > > 
> > > ![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-16-890x1024.png)

Ah, another dependency. Don’t you love those? Let’s get the .NET Core SDK installed.

```
start https://www.microsoft.com/net/download
```

All done. Let’s give it another shot. But first, I need to restart my PowerShell console again and try again.

```
func start
```

…and we’re hitting another error. The red that sticks out to me is “failed to start a new language worker for runtime powershell”. A worker in Azure Functions speak is the host that executes PowerShell. Since we’re running locally, that’s my local machine. Let’s check the local settings JSON file.

```
code C:\PowerShellFunctionProject\local.settings.json
```

By default, it set the runtime version to 7.2. Let’s check the version installed on my machine.

```
$PSVersionTable
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-17-1024x471.png)

Ah, there’s the problem. I’m on 7.4. Let’s change that.

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-18-1024x363.png)

Now, let’s try that again.

```
func start
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-19-1024x338.png)

It’s much better. There’s no ugly red. Now, we have a URL we can trigger the script with. Let’s copy that URL and open another console.

```
Invoke-RestMethod -Uri http://localhost:7071/api/PowerShellFunction
```

I’ll use `Invoke-RestMethod` to query the local web server and see what happens.

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-20-1024x72.png)

Yay! The PowerShell text is shown. But wait, there’s nothing after ‘Hello’.

```
Invoke-RestMethod -Uri <http://localhost:7071/api/PowerShellFunction> -Body @{Name='Adam'}
```

Let’s pass the HTTP body with a key of Name that the script is looking at.

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-21-1024x73.png)

Success!

```
gc C:\PowerShellFunctionProject\PowerShellFunction\run.ps1
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-22-1024x337.png)

You can see where `$Request.Query.Name` is in the script here.

We now know that the function was triggered by the HTTP GET request we just executed to the local webserver. The function ran, executed the PowerShell script, and then sent the PowerShell script output back via an HTTP response.

## Publishing the Azure Function

Our function is complete and tested locally; now let’s publish it to Azure and see if it works up there too. Let me first make sure I’m in my function project directory.

```
cd C:\PowerShellFunctionProject
```

```
Get-AzFunctionApp -Name "PowerShellFunctionsDemo" -ResourceGroupName "PowerShellAzFunctionsDemo"
```

Let me verify the name of the Function app I created earlier.

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-23-1024x249.png)

OK, good. It is PowerShellFunctionsDemo.

```
func azure functionapp publish PowerShellFunctionsDemo
```

![](https://adamtheautomator.com/wp-content/uploads/2024/10/image-24-1024x289.png)

Now I can use the func binary again and publish the function by specifying the name of the function app to push the local function to.

This process zips up all of the files in that function folder, uploads them to the storage account that’s hosting the function app and then deploys them to a little web app.

Looks good. Time for the final test!

```
Invoke-RestMethod -Uri 'https://powershellfunctionsdemo.azurewebsites.net/api/powershellfunction' -Body @{Name='Adam'}
```

Success! It’s now running precisely the same as it was on my local machine. Our Azure Function is deployed and available!

## Conclusion

Congratulations! You’ve successfully created an Azure Function that runs PowerShell code in the cloud. This opens up a world of possibilities for serverless automation and scalable script execution.

Remember, this is just scratching the surface. Azure Functions can do much more, including handling different types of triggers (like timers or queue messages) and integrating with other Azure services. Happy scripting in the cloud!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-function-powershell-cloud%2F&text=Creating%20an%20Azure%20Function%20to%20Run%20PowerShell%20in%20the%20Cloud)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fazure-function-powershell-cloud%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-function-powershell-cloud%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/10/DALL·E-2024-10-27-12.16.31-An-image-representing-the-creation-of-an-Azure-Function-for-running-PowerShell-in-the-cloud.-The-image-includes-a-PowerShell-console-with-commands-to-.webp)

### [Create Your First PowerShell Azure Function: A Step-by-Step Guide](/powershell-azure-function-tutorial/)

Learn how to create, test, and deploy an HTTP-triggered PowerShell Azure Function from scratch using Azure PowerShell and Azure Functions Core Tools in this hands-on tutorial.

![](https://adamtheautomator.com/wp-content/uploads/2026/08/featured_image-3.webp)

### [Build an Azure Home Lab for Certs Without Overspending](/azure-home-lab-certification-practice/)

Build a nested Azure Hyper-V home lab with budget alerts, a domain controller, and Azure Arc mapped to AZ-900, AZ-104, and AZ-305 exams.

![](https://adamtheautomator.com/wp-content/uploads/2026/04/featured_image-16.webp)

### [Microsoft 365 E7: Is the $99/User Price Tag Worth It?](/microsoft-365-e7-99user-price-tag-worth-it/)

Evaluate whether Microsoft 365 E7’s $99 price justifies Agent 365 governance using PowerShell scripts to assess Copilot usage and automation risk exposure.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
