Azure function - Powershell Date.AddDays not working

Madhu Rao 120 Reputation points
2025-06-13T02:13:25.02+00:00

Hi,

I have an Azure function that uses a date span to run a query. I am using the existing date and a (-ve) numeric value.

The below script works on my laptop.

$cldopsemailqrystrtdays = -7  # Set this to how many days back you want to go
$endtime = (Get-Date).Date    # Midnight today
$starttime = $endtime.AddDays($cldopsemailqrystrtdays)
Write-Output "Start Time: $starttime"
Write-Output "End Time:   $endtime"

I am passing an env: variable in Azure Function to pass on a numerical value that would be the start date of my query. The Azure function fails to recognise the start date.

$cldopsemailqrystrtdays=$env:cldopsemailqrystrtdays

$endtime = (Get-Date).Date
$starttime = ($endtime).AddDays($cldopsemailqrystrtdays)

$starttimemsg = $starttime
$endtimemsg = $endtime

Write-Host "$starttime - $endtime"

Thanks in advance.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,867 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.