1

I noticed that there are duplicate user accounts on users' Windows 10 computers that prevents in place upgrade to Windows 11. So I'm trying to remove these user accounts in a "prepare-for-win11-upgrade.ps1" containing so far this:

    #REMOVE SYMLINKS

$users = "testuser", "testuser1", "defaultuser0"

foreach ($users in $users) {

$symlinks = Get-ChildItem -Path "C:\Users\$users\appdata\local\" -Force |
            Where-Object { $_.LinkType -ne $null -or $_.Attributes -match "ReparsePoint" } |
            Select-Object -ExpandProperty FullName

    foreach ($pathtotargetsymlinks in $symlinks){
    unlink $pathtotargetsymlinks -Force
    }
}

#REMOVE USER ACCOUNTS

if (Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq "c:\users\testuser1"}) 
    {
    takeown /f "c:\users\testuser1" /r
    $SIDtoDelete = Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq "c:\users\testuser1"} | Select-Object -ExpandProperty SID
        
        foreach ($SIDtoDelete in $SIDtoDelete)
            {
               Get-CimInstance -ClassName Win32_UserProfile | Where {$_.SID -eq $SIDtoDelete} | Remove-CimInstance -Verbose -Confirm:$false
            }

    }

but it fails with "Remove-CimInstance : the process cannot access the file because it is used by another process" even after reboot. Symlink folders aren't even there anymore. "C:\users\testuser\appdata\local\Application Data" folder doesn't exist, neither its symlink.

Can someone suggest something if how to remove the stuck, useless profiles? Same result on deleting the folders if I start with deleting the correspondent registry keys and rebooting. Which actually is sufficient to perform an in place upgrade but I'd like to be more through.

I'm putting things in foreach because there's a duplicated "testuser1" so I want to make sure it goes through the whole list as many times as has to trying to use SID instead of the name only.

In sysdm.cpl / advanced / user accounts I cannot see the actual names but "Account Unknown" and did already try the googled suggestions (with !!!Account Unknown!!! in it)

Of course I'm running the code as local admin running ISE "Run as Administrator".

2
  • Try booting into safe mode. Also, you shouldnt have fiddled with the profile folders, remove-ciminstance wouldve taken care of everything. Chances are the only way to clean up the mess now is by removing them with regedit.
    – Silbee
    Commented May 24 at 10:37
  • Long story short: My predecessor built the SCCM task sequence images of a poorly set up machine. Above mentioned users are still logged in because fast boot was enabled on that computer. So a shutdown - power on solves it though I'm testing it on a VM I'll have to ask the correspondent team if we have fast boot enabled on physical machines. I'm afraid it is because I have tried rebooting production machines before trying to remove profiles. If so, I'm sure it's easier just to go with clean 11 install. Our users have 3 different cloud storage options, and shouldn't save anything locally anyway.
    – Nash
    Commented May 24 at 17:43

1 Answer 1

2

My case here is resolved, the code above is even an overkill. Script failed because of the locked user accounts caused by a poorly created golden image. Disabling fast boot unlocks the locked profiles and removing desired accounts is a straight forward task. Thanks for guiding me in the right direction @Silbee Here is my full blown script that worked fine in my system. I still don't know how the duplicate accounts got created in the registry.

 $usersfolders = "c:\users\testuser", "c:\users\testuser1", "c:\users\defaultuser0"

foreach ($usersfolders in $usersfolders) 
    {
    if ((Test-Path -Path $usersfolders) -eq "True")
        {
            if (Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq $usersfolders}) 
                {
                    Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq $usersfolders} | Remove-CimInstance -Verbose -Confirm:$false
                }
            if ((Test-Path -Path $usersfolders) -eq "True")
                {
                    Start-Process -FilePath "cmd.exe" -ArgumentList "/c rmdir /q /s $usersfolders"
                    Remove-Item -Path $usersfolders -Recurse -Force
                }
            else
                {
                echo "Folder already removed."
                }
        }    
    else
        {
        echo "Folder doesn't exist."
        }
        if (Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq $usersfolders}) 
                {
                    Get-CimInstance -ClassName Win32_UserProfile | Where {$_.LocalPath -eq $usersfolders} | Remove-CimInstance -Verbose -Confirm:$false
                }
        else
                {
                    echo "No 2nd $usersfolder account found."
                }
    } 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .