Ultimate folder redirection for Onedrive, Teams and Sharepoint

In the post-Onedrivemapper era where we have Files On Demand, there is still room for improvement in client side configuration of Onedrive for Business. Onedrive Known Folders isn’t up to par yet, doesn’t support any customization and there are situations where I want to be able to redirect local folders to other places than Onedrive like Teams or Sharepoint.

Therefore I present to you “Invoke-O4BAutoMount“; the ultimate Onedrive/Sharepoint/Teams sync and redirect solution in modern workplace scenario’s, no WebDav, just the NSG Onedrive Client and native Intune Management Extension:

  • Single PS script
  • User does NOT have to be an Admin
  • Per special folder configuration of redirection settings
  • Extra (symlink) redirection for ANY folder (specific appdata subfolders anyone?)
  • Automatically runs at logon and/or at specified intervals
  • Can be unassigned (removing a user from the target group in Intune stops the script from running)
  • Can redirect to subfolders, even nested
  • Handles automatic creation of any folders
  • Supports variables in paths
  • Registers Environment Variables for the new locations if you configure it to
  • Can hide target folders (appdata likes to hide)
  • Multi-user support (Windows Virtual Desktop / Server 2019)

How to use in 8 easy steps:

1. Configure the tenant ID and how often you want to script to run:

2. Then get the ODOpen URL of any libraries you wish to sync:

Use Chrome or Edge, do not use Internet Explorer!

3. paste the URL into the script in the syncUrl section and configure the site and list titles:

User experience: The site and list title will show in explorer like “siteTitle – listTitle”

4. if you only need Onedrive and don’t want to sync any sharepoint/teams libraries, configure like this:

5. Pick which folders you want to redirect, and where you want to redirect them to:

This example redirects the My Documents and Desktop folders to the user’s Onedrive, and the Pictures folder to a subfolder in a teamsite called “AutoMapTestTeam”, the subfolder is automatically created using the user’s email address.

6. Configure any special folders that normally cannot be redirected

This example will redirect the local Skype folderin APPDATA to a user’s Onedrive, thus roaming Skype contacts and profile information to Onedrive

7. Upload to Intune as a PowerShell script:

Make sure you set it to 64 bit AND to run as system instead of the user’s logged on credentials

8. Deploy to your users (you can also deploy to machines, but then you need to set autoRerunMinutes to > 0 and it might not run at logon)

Further recommendations

This script assumes you’ve already Configured Onedrive to automatically log the user in and I strongly recommend to run Onedrive in Per Machine Mode for faster config.

I haven’t extensively tested running this script when multiple users are active on a device at the same time (ie: citrix, wvd, remoteapp); I’d love to hear about your results!

Thanks and inspiration

Michael Mardahl for giving me the ODOPEN and IME hack examples

Aaron Parker for writing on Folder Redirection with PowerShell

Justin Murray for showing me how to impersonate another user

Per Larsen for writing on silent config / admx stuff regarding Onedrive

Olivier Kieselbach for his three deep dives on the Intune Management Extension


As always, this is a free script and no guarantees are given, use at your own risk.


Log locations:

c:\\users\\appdata\\local\\temp\\Invoke-O4BAutoMount.log for the user portion.

C:\\Windows\\Temp\\Invoke-O4BAutoMount.log for the SYSTEM portion

If the script isn’t running at all (no log file), check the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IntuneManagementExtension\Policies subkeys for error info from the script. Make sure the config section of the script is correct or it won’t run.

If it isn’t as running as frequently as you like, you may be getting throttled. Don’t run more frequently than hourly (per account).

Also check Olivier’s deep dives for general troubleshooting of Powershell scripts with Intune

Leave a Reply

12 Comment threads
18 Thread replies
Most reacted comment
Hottest comment thread
11 Comment authors
ReneJosAndrewAndrew TaylorCollin Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

Hi Jos,

A quick question. If I have users in multiple departments, can the script be used in a cumulative fashion?

IE, if I were to have a base all users script, but I had some users in finance, and some in HR, and a couple across both those functions, could I deploy the script to their security groups and have them syncing all of the libraries they need for their job, or would whichever script ran last over-write the previous scripts?

Andrew Taylor
Andrew Taylor

Great script! What happens if a user has redirected app data and then rebuild the computer? Will it overwrite the previous app data with the new blank one from the rebuild? I’d like to protect signatures, auto text etc. Would it be safer to just not copy existing files?


Heey Jos,

Misschien is een mogelijkheid om aan het script toe te voegen dat de offline bestanden na een tijdje verwijderd worden lokaal. wat de functie opruimen doet van onedrive. er is ook zoon functie in windows maar ik heb nog niet kunnen vinden om dit voor alle computers/users door te voeren dat hij bijvoorbeeld na 3 dagen de offline bestanden opruimd en omzet naar alleen online bestanden.


Hi Jos, great script, thanks for that! I have one ‘issue’ which you might be able to help solve. We have a Team and there are several files and folders in the document library of that Team. When I connect Onedrive using your script, I see all my files and folders INSIDE a folder called ‘General’. Our users than have to open that folder ‘General’ and they will find all files and folders that are also visible via the Teams app. But when they make other folder at the same level of the General folder, no one will be able… Read more »


I Get from intune that the scripts is run, but noting on client.
When i go to c:\windows\temp and have a look at the log i can se there stands.

Running as system, Running in system Context, script shot be running in user context, we should auto impersonate?


Just noticed something strange when running the script, I get “OneDrive not yet fully configured for this user….”
Checking the script there is the line
However checking the registry on my system I have Accounts\Business1 so the test fails and the script fails.

Will see if I can fix and post back.



Martin Joergensen

If i wanted to map multiply folders, it only maps on.

$listOfLibrariesToAutoMount = @(
@{“siteTitle” = “Alle”;”listTitle”=””;”syncUrl” = “tenant…”}
@{“siteTitle” = “Oekonomi”;”listTitle”=””;”syncUrl” = “tenant…”}
@{“siteTitle” = “Personale”;”listTitle”=””;”syncUrl” = “tenant…”}
am i doing something wrong?

Also, i get notifiet all the time that the foldes is allready in sync.
it pops up with 2 min between


Hi Jos,

Create artikel! It is definitely what i was looking for!

If you declare a space within the siteTitle of $listOfLibrariesToAutoMount it gets recplace for a + sign by [System.Web.HttpUtility]::UrlEncode.

If you use [uri]::EscapeDataString it will change to %20 so people see a normall space wihtin explorer.

Is there a particular reason why you use [System.Web.HttpUtility]::UrlEncode?

But again create script! Will proberly use it alot


Thanks for this. Is it possible to have the name be the same as when you hit the Sync button?

For example a document library named Apps in a SharePoint site named Files gets the name Files – Apps when synced manually through the button.

With your script it gets the name Apps – Documents.

I use applications that get certain data from synced SharePoint sites, but I cannot set multiple possible paths to the folder location.


How would this work on a shared computer?


[…] As you may have noticed, I’m using an environment variable here called ${DOCUMENTS}. This variable is a google-specific variable, you can’t use environment variables here like %APPDATA%, if you want to roam to a different location, use my Ultimate redirection script […]


[…] ARTICLE SUPERCEDED BY Ultimate folder redirection for Onedrive, Teams and Sharepoint […]