I’m using Power Automate Desktop and running a custom PowerShell script using the "Run PowerShell Script" action.
Here is the PowerShell script I’m using:
# Get the 1st day of the current month
$now_date = Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Get the last day of the current month
$month_after = (Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year).AddMonths(1).AddDays(-1) -Format "dd-MMM-yy"
# Initialize variables for the previous month's 1st and last days
$prev_month_first = $null
$prev_month_last = $null
# Check if now_date and month_after are the same
if ($now_date -eq $month_after) {
# Calculate the 1st day of the previous month
$prev_month_first = (Get-Date -Day 1 -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).AddMonths(-1).Year) -Format "dd-MMM-yy"
# Calculate the last day of the previous month
$prev_month_last = (Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year).AddDays(-1) -Format "dd-MMM-yy"
}
# Output the values
$now_date, $month_after, $prev_month_first, $prev_month_last
In Power Automate Desktop:
The entire output goes into the %PowerShellOutput% variable.
I want to extract each of the four values ($now_date, $month_after, etc.) and store them into separate variables like %NowDate%, %MonthAfter%, etc.
I tried referencing %now_date% directly but it doesn’t work.
I’m okay with receiving the output as plain text — I just need a way to split it and store it properly.
Question: How do I parse the output of a PowerShell script into multiple new variables in Power Automate Desktop?
Is there a reliable method to separate the values from %PowerShellOutput% and assign them to individual variables?
Thanks in advance!
Your Script produces errors and thus returns a blank.
Enable the ScriptError
variable under the variables produced options.
There are other errors in your script:
The script should be this:
# Get the 1st day of the current month
$now_date = Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Get the last day of the current month
$month_after = Get-Date -Day 1 -Month (Get-Date).AddMonths(1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Initialize variables for the previous month's 1st and last days
$prev_month_first = $null
$prev_month_last = $null
# Check if now_date and month_after are the same
if ($now_date -eq $month_after) {
# Calculate the 1st day of the previous month
$prev_month_first = Get-Date -Day 1 -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
#calculate last day of month
$last_days_in_month = [DateTime]::DaysInMonth((Get-Date).Year, (Get-Date).AddMonths(-1).Month)
# Calculate the last day of the previous month
$prev_month_last = Date -Day $last_days_in_month -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
}
# Output the values
$now_date, $month_after, $prev_month_first, $prev_month_last
Get your script to run correctly in PowerShell, when I tested your script in Windows PowerShell ISE I got the same errors.
Once your script passes you will see the output in %PowershellOutput%
this will be a multiline string, so you could use the Split Text action in power automate desktop to get a list, after which you can get the values according to the index in TextList
variable.
copy and then paste this text to a power automate desktop
Text.SplitText.SplitWithDelimiter Text: PowershellOutput CustomDelimiter: $'''\\n''' IsRegEx: True Result=> TextList
You should get something like this
Get the values using the index position, so %TextList[1]%
will give you the string calculated by $month_after
.
If you wanted to return everything as a single string then you can write your last line something like
Write-Host "$($now_date), $($month_after) ...etc"