Windows 在PowerShell中删除用户配置文件有时会留下配置文件
我创建了我的第一个PowerShell脚本来批量删除用户配置文件,在大多数情况下,它工作正常,但是,当运行Windows 在PowerShell中删除用户配置文件有时会留下配置文件,windows,powershell,user-profile,Windows,Powershell,User Profile,我创建了我的第一个PowerShell脚本来批量删除用户配置文件,在大多数情况下,它工作正常,但是,当运行Get-cimpinstance win32_-UserProfile时,它偶尔会留下一些在PowerShell中找不到的配置文件 尽管PowerShell似乎看不到这些配置文件,但脚本确实会触动它们,使它们的最后访问日期是最新的,除appdata文件夹外的所有内容都会被删除,并且它们仍然位于C:\Users目录中 我目前唯一的解决方案是在用户中手动选择这些配置文件,然后删除它们,但我正在尝
Get-cimpinstance win32_-UserProfile
时,它偶尔会留下一些在PowerShell中找不到的配置文件
尽管PowerShell似乎看不到这些配置文件,但脚本确实会触动它们,使它们的最后访问日期是最新的,除appdata文件夹外的所有内容都会被删除,并且它们仍然位于C:\Users
目录中
我目前唯一的解决方案是在用户中手动选择这些配置文件,然后删除它们,但我正在尝试完全自动化这个过程。我不知道这是什么原因造成的,所以非常感谢您的任何建议或指导。这是我的密码:
# Grab all the user profiles and ignore anything in our exceptions
$Profiles = Get-CimInstance Win32_UserProfile | where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\admin1") -and ($_.LocalPath -ne "C:\Users\admin2") -and ($_.LocalPath -ne "C:\Users\admin3") -and ($_.LocalPath -ne "C:\Users\admin4"))}
# Get the number of profiles to use in -PercentComplete
$ProfilesCount = $Profiles.Count
# This for loop iterates through profiles and deletes them as well as creates our progress bar
Function ProfilesB-Gone
{
for ($i = 1; $i -lt $ProfilesCount; $i++)
{
# Our progress bar is generated and updated
Write-Progress -Activity 'Removing Profiles' -Status "Deleted $i out of $ProfilesCount profiles" -PercentComplete (($i/$ProfilesCount) * 100)
# Here we're suppressing errors and continuing while deleting our profiles
Remove-CimInstance $Profiles[$i] -EV Err -EA SilentlyContinue
}
# Remove progress bar once complete
Write-Progress -Activity 'Removing Profiles' -Status 'Complete!' -Completed
}
# Call function
ProfilesB-Gone;
# Remove all leftover profiles that remain ocasionally due to noncritical and inconsistent bug and suppress errors. Not suppressing errors causes as many error windows to show as there were $ProfilesCount.
$Profiles | Remove-CimInstance -EV Err -EA SilentlyContinue
# Give success message to inform user of script completion
Write-Host "Profiles Deleted!"
win10应用程序(MicrosoftOfficeHub)会生成无法以正常方式删除的奇怪链接。使用类似于cmd/c rmdir/s/q c:\users\user
的方法仍然可以工作,而powershell和wmi则不能 win10应用程序(MicrosoftOfficeHub)会生成奇怪的链接,无法以正常方式删除。使用类似于cmd/c rmdir/s/q c:\users\user
的方法仍然可以工作,而powershell和wmi则不能 我将使用Try…Catch结构,以便每个概要文件在循环期间返回任何错误。我希望AppData中的某些文件由于某种原因无法删除。我将使用Try…Catch结构,以便每个配置文件在循环期间返回任何错误。我希望AppData中的某些文件由于某种原因无法删除。就是这样,谢谢你的链接,因为我对可能导致这种情况的原因失去了理智。我通过powershell解决了您建议的cmd问题。就是这样,谢谢您的链接,因为我对可能导致此问题的原因失去了理智。我通过powershell解决了您建议的cmd问题。