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".