Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

office365 - Office 365 OneDrive for Business reset and remove all local files

(first post)

Is there a better way to do this?

I've thinkered with this batchfile with the purpose "start from scratch" for the client.

  • OneDrive is shutdown and done a reset to.
  • Account info is deleted from registry.
  • Explorer NameSpace UID are removed
  • All local files in the userprofile matching the Tenant name are recycled into the Windows Recycle bin.
  • Explorer process and OneDrive are restarted.

If "OneDrive backup" is active, that is synced Desktop, Documents and Pictures, then they are momentarily lost until OneDrive is activated again.

The recycle CALL was found here on stackoverflow. Thanks to @jomadin for that one. Also the reg query parsing was found here, awesome!

@echo off
setlocal enabledelayedexpansion

if "%1" EQU "DOIT" (
    echo * RUN and DOIT!
    set cmdMD=md
    set cmdREG=reg
    timeout 10
) else (
    echo * TEST RUN (DOIT as argument if to execute)
    set "cmdMD=echo md"
    set "cmdREG=echo reg"
)

FOR /F "tokens=2* skip=2" %%a in ('reg query "HKEY_CURRENT_USERSOFTWAREMicrosoftOneDriveAccountsBusiness1" /v "DisplayName"') do set "strODName=%%b"
echo "%strODName%"
if "%strODName%" EQU "" (
    echo * Tenant name not found.
    goto :eof
)

echo * Operation is done for "%USERNAME%" on "%strODName%"

echo * Shutdown OneDrive
if "%1" EQU "DOIT" %LOCALAPPDATA%MicrosoftOneDriveonedrive.exe /shutdown
timeout 2

echo * Reset OneDrive
if "%1" EQU "DOIT" %LOCALAPPDATA%MicrosoftOneDriveonedrive.exe /reset
timeout 2

echo * Remove Account
%cmdREG% delete "HKEY_CURRENT_USERSOFTWAREMicrosoftOneDriveAccounts" /f

echo * Remove NameSpace for Explorer
set ODNameSpace=
FOR /F "delims=¤" %%i in ('reg query HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerDesktopNameSpace /s /f "%strODName%" /d ^| findstr /i "CURRENT"') do (
    set ODNameSpace=%%i
    if "!ODNameSpace!" NEQ "" (
    echo Found, removing "!ODNameSpace!"
    %cmdREG% delete "!ODNameSpace!" /f
    )
)

echo * Create local folders if missing
if not exist "%USERPROFILE%Desktop" %cmdMD% "%USERPROFILE%Desktop"
if not exist "%USERPROFILE%Pictures" %cmdMD% "%USERPROFILE%Pictures"
if not exist "%USERPROFILE%Documents" %cmdMD% "%USERPROFILE%Documents"
if not exist "%USERPROFILE%PicturesScreenshots" %cmdMD% "%USERPROFILE%PicturesScreenshots"

echo * Restore paths for standard folders if set to OneDrive
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "{0DDD015D-B06C-45D5-8C4C-F59713854639}" /t REG_EXPAND_SZ /d "%USERPROFILE%Pictures" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "{B7BEDE81-DF94-4682-A7D8-57A52620B86F}" /t REG_EXPAND_SZ /d "%USERPROFILE%PicturesScreenshots" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "{F42EE2D3-909F-4907-8871-4C22FC0BF756}" /t REG_EXPAND_SZ /d "%USERPROFILE%Documents" /f

%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "Desktop"     /t REG_EXPAND_SZ /d "%USERPROFILE%Desktop" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "My Pictures" /t REG_EXPAND_SZ /d "%USERPROFILE%Pictures" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" /v "Personal"    /t REG_EXPAND_SZ /d "%USERPROFILE%Documents" /f

rem old key HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerShell Folders
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerShell Folders" /v "Desktop"     /t REG_SZ /d "%USERPROFILE%Desktop" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerShell Folders" /v "My Pictures" /t REG_SZ /d "%USERPROFILE%Pictures" /f
%cmdREG% add "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerShell Folders" /v "Personal"    /t REG_SZ /d "%USERPROFILE%Documents" /f

echo * Remove local synced files
echo * "%USERPROFILE%OneDrive - %strODName%"
echo * "%USERPROFILE%\%strODName%"

if "%1" EQU "DOIT" (
    if exist "%USERPROFILE%OneDrive - %strODName%" CALL :ToRecycleBin "%USERPROFILE%OneDrive - %strODName%"
    if exist "%USERPROFILE%\%strODName%" CALL :ToRecycleBin "%USERPROFILE%\%strODName%"

    echo * Done.
    
    echo * Restarting Explorer.exe
    timeout 3
    taskkill /im explorer.exe /f
    timeout 3
    start "" explorer.exe
    timeout 10
    echo * Restarting OneDrive
    start "" %LOCALAPPDATA%MicrosoftOneDriveonedrive.exe
)
timeout 10
endlocal
goto :eof

:ToRecycleBin
rem echo "%~f1"
echo (new-object -comobject Shell.Application).Namespace(0).ParseName("%~f1").InvokeVerb("delete") | powershell -command -
exit /b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...