locked
Remove OneDrive Powershell RRS feed

  • Question

  • Building a PowerShell script to remove OneDriveSetup from the registry. Each time I attempt to unload the UserHive, I get an "Error: Access is denied."

    How am I unable to unload this hive?

    ##### Remove OneDriveSetup from Registry
    
    $regHKUPath = "HKU:\UserHive\Software\Microsoft\Windows\CurrentVersion\Run"
    $element = "C:\Users\Default"
    
    $hideMe = New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
    $hideMe = reg load HKU\UserHive "$element\NTUSER.DAT"
    
    If (Get-ItemProperty -Path $regHKUPath -Name OneDriveSetup -ErrorAction SilentlyContinue) {
    
        Write-Output 'OneDriveSetup Value exists'
        Get-Item -Path $regHKUPath | Remove-ItemProperty -Name OneDriveSetup
    } Else {
        Write-Output 'OneDriveSetup Value DOES NOT exist'
    }
    
    [gc]::Collect()
    
    $hideMe = reg unload HKU\UserHive
    $hideMe = Remove-PSDrive -Name HKU
    
    ##### End Remove OneDriveSetup from Registry

    Thursday, July 23, 2020 11:52 PM

All replies

  • You have to wait until the system unloads the drive but first un-map the drive

    Remove-PSDrive -Name HKU -Force
    sleep 30
    reg unload HKU\UserHive


    \_(ツ)_/

    • Proposed as answer by Ian Xue Wednesday, July 29, 2020 8:40 AM
    Friday, July 24, 2020 12:19 AM
  •  Hi, Just want to confirm the current situations. If the above reply is helpful, please mark it as answer to help other community members find it quickly.

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, July 29, 2020 8:49 AM