Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Template
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
Menu

How to select a random image from OneDrive with Power Automate

Posted on July 20, 2022July 20, 2022 by Tom

“I’d like to select a random image from my OneDrive every day and post it on Twitter, can I do it with Power Automate?”


When you build flows in Power Automate, you probably base them on a specific input. A specific Excel file that contains Planner tasks, specific SharePoint item with an approval process, or an email with specific attachment. But sometimes you don’t need a specific row/file/item. You might want a random input instead, e.g. a random image from a folder, a random row from an Excel file, or a random news. For example, to build a flow that’ll post a random funny picture on your Twitter every day.

Get all the available inputs

Before you can select a random input, you must first know what the possible inputs are. For this example it’ll be all the images in the OneDrive folder /Images.

Get the position of the random input

The next step is to calculate how many images you’ve got. Use the length(…) expression to get the number of available images.

length(outputs('List_files_in_folder')?['body/value'])

This number will give you the top limit. If you imagine the inputs as a list of files, the length is the last possible position for a file, while 0 is the first possible position. Use the rand(…) expression to get a random position between 0 and that number.

rand(0,length(outputs('List_files_in_folder')?['body/value']))

The random number can be then used as a position of the random image.

Get the input itself

At this moment you’ve get everything you need. You’ve got the list of all the possible inputs, and you have a randomly selected position of one of them. Since the inputs are a JSON array, you can use the position as the index.

outputs('List_files_in_folder')?['body/value'][<index>]

or

outputs('List_files_in_folder')?['body/value'][rand(0,length(outputs('List_files_in_folder')?['body/value']))]

All that’s left is to take the required value from the input, in this case the image id to get the file content. Once you have the content you can post it on Twitter.

outputs('List_files_in_folder')?['body/value'][<index>]?[<property>]

or

outputs('List_files_in_folder')?['body/value'][rand(0,length(outputs('List_files_in_folder')?['body/value']))]?['Id']
Power Automate select random

Summary

There’re three main points in this article. Firstly, you can use Power Automate even to select a random value for you, you don’t have to pick a specific entry every time. In this example it was a random image, but you can randomly select and process anything. Secondly, it’s another example of combining expressions. You could use multiple ‘Compose’ actions to build each part of the expression, or you can do everything at once. And thirdly, Power Automate is great in the Microsoft 365 platform, but it’s not limited to it. If you start using connections to other tools/apps you can do so much more.


πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

3 thoughts on “How to select a random image from OneDrive with Power Automate”

  1. Andry says:
    April 16, 2023 at 9:53 am

    Hi. Your solution How to select a random image from OneDrive with Power Automate helped me a lot. Could you also give me a tip on how to move the image after a tweet post to a new OneDrive folder

    Reply
  2. Andry says:
    April 16, 2023 at 11:34 am

    you need to use an expression that will select a file from the list of files in folder in order. Here’s how you can do it:

    Instead of the triggerBody() expression?[‘$foreach’][‘item’]?[‘Index’], use the intDiv() function to divide the current loop variable value by 1:

    intDiv(triggerBody()?[‘$foreach’][‘item’], 1)

    This will allow you to get the current file index in the list of files in the folder

    Use this expression instead of the previous one to select the file in order:

    outputs(‘List_files_in_folder’)?[‘body/value’][intDiv(triggerBody()?[‘$foreach’][‘item’], 1)][‘Id’]

    This expression will select a file from the list of files in the folder in order, using the current index from the loop variable.

    Reply
    1. Tom says:
      May 1, 2023 at 3:44 pm

      Hello Andry,
      thank you for sharing the solution.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Still exchanging emails to get things approved?

Use the Approval Process Template and the Task Delegation App to skip the hard part and deploy an automated, fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview ✨ Learn more ✨

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundleβ€”everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview ✨ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes