Skip to main content

Redirect Windows folder to OneDrive via PowerShell

This PowerShell script is only usable for Windows

Use case

The following script lets you redirect the main folders of your Windows Explorer. This will ensure that all files and data are stored in the OneDrive storage solution managed by the company.

The only thing that needs to be manipulated is the "$CompanyName" variable, which requires the name of the company configured in Microsoft 365.

Script Code

This code must run on the computer in the context of the user.

$CompanyName = "LNC Freelancing"

$OneDrivePath = "$env:USERPROFILE\OneDrive - $CompanyName"

# Create New Folder
New-Item "$OneDrivePath\Desktop" -Type Directory -Force
New-Item "$OneDrivePath\Dokumente" -Type Directory -Force
New-Item "$OneDrivePath\Bilder" -Type Directory -Force
New-Item "$OneDrivePath\Videos" -Type Directory -Force
New-Item "$OneDrivePath\Musik" -Type Directory -Force

# Redirect the Desktop folder
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}" -Value "$OneDrivePath\Desktop" -PropertyType "ExpandString" -Force

# Redirect the Documents folder
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}" -Value "$OneDrivePath\Dokumente" -PropertyType "ExpandString" -Force

# Redirect the Pictures folder
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{33E28130-4E1E-4676-835A-98395C3BC3BB}" -Value "$OneDrivePath\Bilder" -PropertyType "ExpandString" -Force

# Redirect the Videos folder
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}" -Value "$OneDrivePath\Videos" -PropertyType "ExpandString" -Force

# Redirect the Music folder
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{4BD8D571-6D19-48D3-BE97-422220080E43}" -Value "$OneDrivePath\Musik" -PropertyType "ExpandString" -Force