MS Graph API scripts for M365
Say hello to my script collection for spinning up fake M365 assets! Time to add a splash of personality to that so boring contoso.com demo tenant and transform your tests from "meh" to marvelous!
At work, I often find myself creating, tweaking, or zapping fake M365 assets for trials, demos or simply testing, and let’s be honest, it’s not exactly a thrill ride.
So why waste precious hours manually entering data when scripts can come to the rescue? They're faster, tidier, and inject some genuine fun into the process.
Let’s breathe some life into those dull contoso.com Microsoft 365 dev domains!
Below you'll find a handy collection of PowerShell scripts designed to play nicely with the MS Graph API and easily manage your M365 data. Each script comes with its own detailed walkthrough, so you can quickly spot and grab the ones that pique your curiosity.
Less clicking, more automating!
List of scripts (PowerShell) :
- Create random fake real-life email in user's inbox
- Count number of email and size of a user's mailbox
- Create random users in EntraID
- Create fake files in OneDrive
- Create fake files in SharePoint
- Count number of file in a SharePoint site
So folks, feel free to grab whatever fits your needs!
1/ Create random fake real-life email in user's inbox
This script populates a user's mailbox with a random set of emails, without triggering Microsoft's SMTP alarms. Instead of mass-sending emails the traditional way (which Microsoft keeps under strict watch), it cleverly drops them straight into the inbox as drafts using MS Graph API.
Your only bottleneck ? Graph API throttling, because even automation has limits !
Features :
- Powershell v5, MS Graph SDK need to be installed : Install-Module Microsoft.Graph (or use PowerShell v7)
- Connect to Graph API using enterprise APP
- Manage MS throttling response code
- Maintain session active
- Number of emails generated randomly within a range
- Randomized dataset:
- Sender name (From): A combination of a real-life random first name and last name
- Email body: Generated using random words
- Attached file: randomly assigning file sizes to attachments.
It picks from a specified array using a probability distribution, so test files mimic real-world randomness - Subject: Composed of sender name + a unique UID
- Required Azure App permissions are:
- Mail.ReadWrite
- Mail.Send
- MailboxFolder.ReadWrite.All
- MailboxSettings.ReadWrite

2/ Count number of email and size of a user's mailbox
A simple yet handy script that counts the number of emails in a user’s inbox and reports the overall mailbox size.
Features :
- Count account size
- Use device authentication
- Required Azure App permissions are:
- Mail.Read (Application Permission)

3/ Create random users in EntraID
This script quickly populates your Entra ID with a large batch of users using Graph API.
Users are generated with a "firstname+lastname+4digit" pattern for both display name and email. Each account is tagged with a specific country to help with automation afterward (like Dynamic Group rules).
Features:
- Powershell v5, MS Graph SDK need to be installed : Install-Module Microsoft.Graph (or use PowerShell v7)
- Connect to Graph API using enterprise APP
- Generate a unique name based on "firstname+lastname+4digit" pattern
- Number of users to create in bulk
- Each user has been given an email address based on their name
- Each user has been assigned a country
- Required Azure App permissions are:
- User.ReadWrite.All (Application Permission)
- Directory.ReadWrite.All (Application Permission)
- MailboxSettings.ReadWrite (Delegated Permission)

4/ Create fake files in OneDrive
This script floods a user's OneDrive with folders and files. Perfect for stress tests or demo setups.
You control the number of folders and items per folder via command-line parameters. File sizes are randomly generated, and filenames follow a unique UID pattern.
Features:
- Powershell v5, MS Graph SDK need to be installed : Install-Module Microsoft.Graph (or use PowerShell v7)
- Connect to Graph API using enterprise APP
- Choose how many folders you want to create
- Decide how many files you want in each folder
- Required Azure App permissions are:
- Files.ReadWrite.All (Application Permission)
- Sites.ReadWrite.All (Application Permission)

You can also pass parameter in the command-line like :
fake_OD_files2.ps1 -MaxFolders 1 -MaxFilesPerFolder 1
5/ Create fake files in SharePoint
This script loads up a SharePoint site with a bunch of folders and files, giving your tenant some content to chew on.
Each folder gets a fun, human-readable name built from a random combo of Color + Adjective + Animal (Some of these names are downright hilarious! 🤣).
Inside, you’ll find a random number of files, each named with a UID and sized randomly up to a max size you define via parameter in the script.
Quick, flexible, and way more fun than looking at an empty document library.
Features:
- Powershell v5, MS Graph SDK need to be installed : Install-Module Microsoft.Graph (or use PowerShell v7)
- Connect to Graph API using enterprise APP
- Manage MS throttling response code
- Generate folders with easy-to-read random names
- Create randomly named files with random sizes
- Required Azure App permissions are:
- Sites.ReadWrite.All (Application Permission)
- Files.ReadWrite.All (Application Permission)
- Directory.Read.All (Application Permission)

6/ Count number of file in a SharePoint site
This script does a quick sweep through all folders in a SharePoint document library and counts how many items each one holds.
So you always have a clear view of how many items your automation is pumping out.
Better to have a clear view to understand the load on your SharePoint site and keep things under control before the chaos takes over!
Features:
- Powershell v5, MS Graph SDK need to be installed : Install-Module Microsoft.Graph (or use PowerShell v7)
- Connect to Graph API using enterprise APP
- Manage MS throttling response code
- Control how deep the scan of the folder structure goes in the settings
- Required Azure App permissions are:
- Sites.ReadWrite.All (Application Permission)
- Files.ReadWrite.All (Application Permission)
- Directory.Read.All (Application Permission)

So here it is, my dear tech friends, I'm excited to share these scripts with you to breathe some life into your otherwise sleepy contoso.com tenant!
Personally, I’ve set them up to run daily via Windows Task Scheduler. That way, my demo tenant gets a fresh dose of "activity" every day, automatically, no manual effort needed.😎
Now sit back, relax, and watch your contoso.com environment evolve like a haunted office where ghost workers are hard at work! 👻💻