Ultimate folder redirection for Onedrive, Teams and Sharepoint

Update: a more lightweight/simpler version of this script is available here: https://www.lieben.nu/liebensraum/2021/09/redirecting-anything-to-onedrive-for-business/

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!
Edit: some users report that this link is gone, check the script source for a workaround 🙂

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

Lightweight version

2021/9: I’ve added a more lightweight version that supports redirection only (with custom PS commands supported). The redirections have to be configured through the registry, see the code here.

Notice

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

Troubleshooting

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

Subscribe
Notify of
guest

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

64 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Brian
4 years ago

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?

Sjaak
Sjaak
4 years ago

Hi Jos, great script! We are testing it right now. But after the client reboots, the Library sync is gone. We have this with all the clients.
Can’t find why..

Any idea?

The personal Onedrive sync will be fine after reboot. It’s just the shared library sync.
And if a set the library sync manually (via sharepoiint itself), the sync stay after reboot.

Jeff
Jeff
3 years ago

Hey Jos,

Is there a way to stop the script from running? I’ve deleted it from Intune but on a test device the script is still looping and failing because the test user doesn’t have access to one of the libraries which I’ve defined.

My idea was to have one script with all of our shares and was hoping the script would loop through and add the libraries the users has permissions for and skip any which the user does not have access to.

Is this possible?

Thanks,

Jeff

Martin
4 years ago

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?

trackback

[…] Ultimate folder redirection […]

Full HD
Full HD
2 years ago

The impersonation no longer appears to work on 20H2 has anyone else experienced this?

If i turn off run as system the script works.

If i turn on run as system. The log appears in C:\Windows\Temp.. but never runs as each user at logon.

Is there an update / fix?

Thank you!

G Wendt
G Wendt
3 years ago

Everything seems to work for m except for the impersonation part. I have to run it under an admin account or it doesn’t work.

Wayne
Wayne
3 years ago

Having some issues with duplication of libraries and a ‘AutoMapTestTeam’ library that I can’t seem to remove.

Is there a way to clear all previously sync’d libraries and start over?

Thanks – Great tool by the way.

Wayne

SRasschaert
SRasschaert
3 years ago

Please cancel all my comments, I made a typo 🙂

SRasschaert
SRasschaert
3 years ago

Hi

First of all, I want to thank you for this great script and your contribution to the community!

I wanted to let you know that I am running into an error.
I configured the list of Libraries as indicated in you manual, because I do not want to syn any libraries.
$listOfLibrariesToAutoMount = @()

However, I receive an error at the following line,

$targetPath = Join-Path -Path (Get-Item “$($Env:USERPROFILE)\$companyName\$compositeTitle”).FullName -ChildPath $redirection.targetPath

Error: Get-Item : Cannot find path ‘C:\Users\xxxxxx\%COMPANY%\ – ‘ because it does not exist.

Last edited 3 years ago by SRasschaert
Iain
Iain
3 years ago

I’m seeing an issue in our environment when signing into a new Azure AD joined device where the script appears to run successfully, but the users folders are not redirected to OneDrive. I think the issue may be that we previosuly did folder redirection via a GPO when devices were Hybrid joined, meaning all the folders we want to redirect now via the script are already present in the users OneDrive. What would be a good solution for that?

Martin
3 years ago

Hi.

Is it posibel to have the script to remove old folders?

i try to set up for some folders on one document libery, but know we have decided to make a hole new setup on a new document libery.

But in my test i have removede the older foldes but it dont remove it?

any good ideers?

Jeff
Jeff
3 years ago

Hi there,
I love the idea of this but I can’t seem to get it to work. I followed the steps and left the powershell script clean (other than the proper tenantID). It will log my test user in but it pops up a window of Microsoft OneDrive and it just says Loading forever. If I close it, it will re just reopen and never load anything. What I’m really trying to do is redirect the Video folder but that doesn’t seem to work either…help?

PacketThrower
PacketThrower
3 years ago

This script is fantastic, exactly what I needed! I have one question.. I have a client whom has over 18 sites, and some users may or may not have access to all. Currently I’ve added 2 sites, one ‘testuser’ has access to and another he doesn’t. it keeps on continuously generating the need to add in that site.. Is there a way to close that loop is that person doesn’t have permission? I’d rather not create 41 Posh scripts :).

Martin
3 years ago

Hi. So great a script.
we just see some problems if a user dont have access to a sharepoint folder.
Then the scripts keeps going in a loop telling onedrive dont have access to the folder..
is there a fix for this?

Tarik
Tarik
4 years ago

Hi Jos,

Thanks for the script. I’m getting the following error, any ideas?

Sending ODOpen command…
start : This command cannot be run due to the error: Application not found.
At C:\Users\xxx\Desktop\Invoke-O4BAutoMount.ps1:642 char:17
+ … start “odopen://sync/?$($library.syncUrl)&userEmail=$([ur …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Rene
Rene
4 years ago

Hallo Jos,

Is het ook mogelijk om Bovenaf dus in ons geval per school het te koppellen? nu zou ik elke bibliotheek los moeten doen wat een hoop werk is. zou het ook kunnen dat ik de school site toevoeg en dat ze daaronder dan alle bibliotheken ziet?

ik kan dat wel doen met de Onedrivemapper zodat ik alleen de school site hoefde in te geven en dat ze op basis van rechten de onderliggende bibliotheken zagen.

Andrew Taylor
Andrew Taylor
4 years ago

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?
Thanks

Rene
Rene
4 years ago

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.

Collin
Collin
4 years ago

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 »

Raj
Raj
4 years ago

Hi,
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
if($account.GetValue(“Business”)
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.

Thanks

Raj

Martin Joergensen
4 years ago

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

Michael
Michael
4 years ago

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

Sasaus
Sasaus
4 years ago

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.

Testy
Testy
4 years ago

How would this work on a shared computer?

trackback

[…] 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 […]

trackback

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