D'abord, du contexte :
Je prépare environ 20 portables pour un programme éducatif en prison. Cet environnement a des défis particuliers. Les étudiants (détenus) ne peuvent en aucun cas avoir accès à quoi que ce soit ressemblant à un email ou une messagerie.
Ils ne peuvent donc pas avoir de comptes Microsoft et n'auront pas accès à un domaine AD ou Entra Identity ; seuls les comptes locaux conviennent (la suppression des messages insistants pour le compte MS sera une autre question), et la gestion par GPO/SCCM/Intune/MDM est exclue.
Ils ne peuvent pas non plus avoir OneDrive sur l'ordinateur. Si un étudiant parvenait à ouvrir OneDrive, il pourrait créer un dossier partagé avec l'extérieur et échanger des messages via des fichiers texte.
*Oui, nous devons être aussi prudents, et les conséquences d'un échec sont des sanctions auxquelles je préfère ne pas penser.*
De plus, même si OneDrive sera bloqué sur le réseau de la prison, les machines seront inspectées avant livraison et la présence de OneDrive ferait échouer l'inspection.
Il n'y a pas de vrai support technique sur site. Le coordinateur éducatif reçoit un compte admin local et des instructions pour créer des utilisateurs locaux. Le processus doit donc fonctionner par défaut pour les nouveaux utilisateurs.
J'ai un lot de machines, avec potentiellement d'autres à venir. Je pars d'installations Windows fraîches, même modèle actuellement, mais le prochain lot sera probablement différent. Je voudrais que ce soit scripté et reproductible.
En bref ([trop tard](https://www.youtube.com/watch?v=YCP2QQH4uuQ)), je veux m'assurer que OneDrive est totalement supprimé de façon reproductible ; le script final tournera probablement quotidiennement en tâche planifiée. C'est pour tous les utilisateurs, y compris ceux pas encore créés.
Jusqu'ici j'ai ceci :
```
# Make sure OneDrive isn't later changed to be a Windows Store app
Get-AppxPackage -AllUsers "*OneDrive*" | Remove-AppxPackage -AllUsers
# But really OneDrive uses any of five possible locations:
# Windows\System, Windows\System32, Windows\SysWOW64, Program Files, and Program Files (x86)
# Need to check all five
if (Test-Path "C:\Windows\System\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\System32\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System32\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\SysWOW64\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\SysWOW64\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
# Program Files locations also have a version number subfolder that may change over time
if (Test-path "C:\Program Files\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
if (Test-path "C:\Program Files (x86)\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files (x86)\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
# Also look in WinSXS
$ods = get-childitem "C:\Windows\WinSxS\amd64_microsoft-windows-onedrive-setup*"
if ($ods) {
$path = "C:\Windows\WinSxS\$($ods.Name)\*.*"
# Don't need to uninstall; just remove files. But also need to take ownership first
takeown /F $path /A | Out-Null
icacls $path /grant Administrators:M | Out-Null
remove-item -path $path -force
}
```
J'inclus ce script pour montrer ce qui est fait, pas pour demander de l'aide avec le code.
J'ai aussi la possibilité d'ajouter des modifications au registre du profil utilisateur par défaut :
```
reg load HKU\default c:\users\default\ntuser.dat
reg import .\DefaultRegistryFixes.reg
reg unload HKU\default
```
Ainsi tout dans `DefaultRegistryFixes.reg` référençant `HKEY_USERS\default` sera écrit dans les paramètres des nouveaux utilisateurs. Le fichier contient environ 40 modifications. Rien sur OneDrive pour l'instant, car il doit être complètement supprimé, pas juste désactivé. Mais toute protection supplémentaire via le registre est bienvenue.
Mon problème :
Après tout ça, OneDrive est absent du compte admin initial. Mais si je crée un nouvel utilisateur local et me connecte, OneDrive apparaît dans la zone de notification. Le Gestionnaire des tâches montre le .exe dans le dossier App Data du nouvel utilisateur.
Qu'est-ce qui me manque ? Comment supprimer OneDrive définitivement ?
Supprimer définitivement et complètement OneDrive pour tous les utilisateurs sous Windows 11 via PowerShell
Re: Supprimer définitivement et complètement OneDrive pour tous les utilisateurs sous Windows 11 via PowerShell
J'ai maintenant réussi à supprimer OneDrive complètement avec un script et un fichier reg. Le script :
```
Write-Host "Removing OneDrive"
# First make our registry tweaks. To do this, we need the default user's hive loaded
reg load HKU\default c:\users\default\ntuser.dat
# Now, before importing changes, look for the OneDrive Personal installer location (we'll need it later)
# Also, KHU isn't available by default, so check that, too
if ((Get-PSDrive -PSProvider Registry).Name -notcontains 'HKU') {
New-PSDrive HKU Registry HKEY_USERS | Out-Null
$removeHKU = $true
}
$odp = (Get-ItemProperty -path 'HKU:\default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run').OneDriveSetup
if ($removeHKU) {
Remove-PSDrive -Name "HKU"
}
# Import our registry changes
# This removes the command to install OneDrive again from the default user template
reg import .\DefaultRegistryFixes.reg
reg unload HKU\default
# Done with registry
# Make sure OneDrive isn't later changed to be a Windows Store app
Get-AppxPackage -AllUsers "*OneDrive*" | Remove-AppxPackage -AllUsers
# OneDrive is installed in one or more of five possible locations:
# Windows\System, Windows\System32, Windows\SysWOW64, Program Files, and Program Files (x86)
# Need to check all five
# First, the Windows locations
if (Test-Path "C:\Windows\System\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\System32\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System32\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\SysWOW64\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\SysWOW64\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
# Then Program Files.
# These locations also have a version number subfolder that may change over time
# So we need extra steps to find that folder
if (Test-path "C:\Program Files\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
if (Test-path "C:\Program Files (x86)\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files (x86)\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
# Now look in WinSXS for the setup file backup
$ods = get-childitem "C:\Windows\WinSxS\amd64_microsoft-windows-onedrive-setup*"
if ($ods) {
$path = "C:\Windows\WinSxS\$($ods.Name)\*.*"
# Don't need to uninstall. Just remove the files. But also need to take ownership first
takeown /F $path /A | Out-Null
icacls $path /grant Administrators:M | Out-Null
remove-item -path $path -force | out-null
}
# And finally clean up installer program found earlier in the registry
if ($odp)
{
$odp = $odp.Replace(" /thfirstsetup", "") # remove command line argument
takeown /F $odp /A | Out-Null
icacls $odp /grant Administrators:M | Out-Null
remove-item $odp | out-null
}
```
Le fichier de registre :
```
Windows Registry Editor Version 5.00
[HKEY_USERS\default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"OneDriveSetup"=-
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"OneDriveSetup"=-
"OneDrive"=-
```
Rappelez-vous que le script a chargé la ruche utilisateur par défaut pour que ce fichier .reg soit effectif.
Cela supprime OneDrive d'un **nouveau** système avec succès. Si des utilisateurs existent déjà, du travail supplémentaire est nécessaire, mais ce n'est pas mon cas.
***Mais je n'ai pas fini.***
Le script sera efficace un à trois mois, puis une mise à jour Windows patchera l'installateur et le registre par défaut. C'est un résultat naturel de la plateforme de maintenance.
Pour empêcher le retour de OneDrive, ce code sera inclus comme tâche planifiée récurrente. Je cherche aussi des modifications de registre supplémentaires pour le rendre moins agressif pendant la période où il pourrait exister.
J'envisage aussi d'écrire des fichiers de remplacement sur les OneDriveSetup.exe avec des ACL empêchant l'écriture par SYSTEM ou TRUSTED INSTALLER. Mais cela pourrait casser les mises à jour majeures.
Enfin, des ajustements manuels de politique de sécurité pourraient bloquer les installateurs, mais je garde ça en dernier recours car ce n'est pas facilement scriptable.
```
Write-Host "Removing OneDrive"
# First make our registry tweaks. To do this, we need the default user's hive loaded
reg load HKU\default c:\users\default\ntuser.dat
# Now, before importing changes, look for the OneDrive Personal installer location (we'll need it later)
# Also, KHU isn't available by default, so check that, too
if ((Get-PSDrive -PSProvider Registry).Name -notcontains 'HKU') {
New-PSDrive HKU Registry HKEY_USERS | Out-Null
$removeHKU = $true
}
$odp = (Get-ItemProperty -path 'HKU:\default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run').OneDriveSetup
if ($removeHKU) {
Remove-PSDrive -Name "HKU"
}
# Import our registry changes
# This removes the command to install OneDrive again from the default user template
reg import .\DefaultRegistryFixes.reg
reg unload HKU\default
# Done with registry
# Make sure OneDrive isn't later changed to be a Windows Store app
Get-AppxPackage -AllUsers "*OneDrive*" | Remove-AppxPackage -AllUsers
# OneDrive is installed in one or more of five possible locations:
# Windows\System, Windows\System32, Windows\SysWOW64, Program Files, and Program Files (x86)
# Need to check all five
# First, the Windows locations
if (Test-Path "C:\Windows\System\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\System32\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\System32\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
if (Test-Path "C:\Windows\SysWOW64\OneDriveSetup.exe") {
start -wait -filepath "C:\Windows\SysWOW64\OneDriveSetup.exe" -argumentlist "/uninstall","/qn"
}
# Then Program Files.
# These locations also have a version number subfolder that may change over time
# So we need extra steps to find that folder
if (Test-path "C:\Program Files\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
if (Test-path "C:\Program Files (x86)\Microsoft OneDrive") {
$folder = (get-childitem "C:\Program Files (x86)\Microsoft OneDrive").Name | ?{ $_ -match "^\d{2}.*" }
# can be several folders. Only one will have the uninstaller
$folder | % {
if (Test-Path "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe") {
$path = "C:\Program Files (x86)\Microsoft OneDrive\$_\OneDriveSetup.exe"
start -wait -filepath $path -argumentlist "/uninstall","/qn"
}
}
}
# Now look in WinSXS for the setup file backup
$ods = get-childitem "C:\Windows\WinSxS\amd64_microsoft-windows-onedrive-setup*"
if ($ods) {
$path = "C:\Windows\WinSxS\$($ods.Name)\*.*"
# Don't need to uninstall. Just remove the files. But also need to take ownership first
takeown /F $path /A | Out-Null
icacls $path /grant Administrators:M | Out-Null
remove-item -path $path -force | out-null
}
# And finally clean up installer program found earlier in the registry
if ($odp)
{
$odp = $odp.Replace(" /thfirstsetup", "") # remove command line argument
takeown /F $odp /A | Out-Null
icacls $odp /grant Administrators:M | Out-Null
remove-item $odp | out-null
}
```
Le fichier de registre :
```
Windows Registry Editor Version 5.00
[HKEY_USERS\default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"OneDriveSetup"=-
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"OneDriveSetup"=-
"OneDrive"=-
```
Rappelez-vous que le script a chargé la ruche utilisateur par défaut pour que ce fichier .reg soit effectif.
Cela supprime OneDrive d'un **nouveau** système avec succès. Si des utilisateurs existent déjà, du travail supplémentaire est nécessaire, mais ce n'est pas mon cas.
***Mais je n'ai pas fini.***
Le script sera efficace un à trois mois, puis une mise à jour Windows patchera l'installateur et le registre par défaut. C'est un résultat naturel de la plateforme de maintenance.
Pour empêcher le retour de OneDrive, ce code sera inclus comme tâche planifiée récurrente. Je cherche aussi des modifications de registre supplémentaires pour le rendre moins agressif pendant la période où il pourrait exister.
J'envisage aussi d'écrire des fichiers de remplacement sur les OneDriveSetup.exe avec des ACL empêchant l'écriture par SYSTEM ou TRUSTED INSTALLER. Mais cela pourrait casser les mises à jour majeures.
Enfin, des ajustements manuels de politique de sécurité pourraient bloquer les installateurs, mais je garde ça en dernier recours car ce n'est pas facilement scriptable.