使用 PowerShell 和 CIM 删除用户配置文件

A common pain point in an IT administrator’s career is user profiles. User profiles are a ubiquitous part of a Windows IT pro’s life; especially those that manage virtual desktop environments like Remote Desktop Services (RDS) or Citrix. In this post, I’m going to demonstrate a way you take out your aggression by using PowerShell to delete user profiles (and discover them).

系统管理员必须处理的问题包括:

  • 损坏的用户注册表hive
  • 需要在所有用户配置文件之间共享的文件
  • 弄清如何重新创建损坏的配置文件
  • …以及更多

曾经令人沮丧的经历现在通过PowerShell变得稍微不那么令人沮丧。以下是 PowerShell 可以使管理 Windows 用户配置文件变得更容易的几种方法。

使用 ManageEngine ADManager Plus 管理和报告 Active Directory、Exchange 和 Microsoft 365。 立即下载免费试用!

枚举用户配置文件

在单个 Windows 计算机的文件系统上查看用户配置文件很容易。只需查看C:\Users文件夹。但是,当您这样做时,您不仅没有获得完整的图片,而且由于潜在的文件系统访问问题,这也很麻烦。有一个更好的方法,那就是通过 WMI 或 CIM。在 CIM 中,存在一个称为Win32_UserProfile的类。该类包含机器上存在的所有配置文件以及许多其他有用信息,这是简单的文件系统文件夹所无法告诉您的。

使用 PowerShell,您可以使用Get-CimInstance命令访问此 CIM 类。

下面,我正在查找本地计算机上的第一个用户配置文件。您将注意到许多有用的信息,如LastUseTimeSID等。

PS C:\> Get-CimInstance -ClassName win32_userprofile | Select-Object -First 1


AppDataRoaming                   : Win32_FolderRedirectionHealth
Contacts                         : Win32_FolderRedirectionHealth
Desktop                          : Win32_FolderRedirectionHealth
Documents                        : Win32_FolderRedirectionHealth
Downloads                        : Win32_FolderRedirectionHealth
Favorites                        : Win32_FolderRedirectionHealth
HealthStatus                     : 3
LastAttemptedProfileDownloadTime :
LastAttemptedProfileUploadTime   :
LastBackgroundRegistryUploadTime :
LastDownloadTime                 :
LastUploadTime                   :
LastUseTime                      : 3/14/2019 3:06:39 PM
Links                            : Win32_FolderRedirectionHealth
Loaded                           : False
LocalPath                        : C:\Users\.NET v4.5 Classic
Music                            : Win32_FolderRedirectionHealth
Pictures                         : Win32_FolderRedirectionHealth
RefCount                         :
RoamingConfigured                : False
RoamingPath                      :
RoamingPreference                :
SavedGames                       : Win32_FolderRedirectionHealth
Searches                         : Win32_FolderRedirectionHealth
SID                              : S-1-5-82-3876422241-1344743610-1729199087-774402673-2621913236
Special                          : False
StartMenu                        : Win32_FolderRedirectionHealth
Status                           : 0
Videos                           : Win32_FolderRedirectionHealth
PSComputerName                   :

Get-CimInstance cmdlet 不仅可以在本地工作,还可以远程工作。通过使用ComputerName参数,您可以指定 1、10 或 100 个不同的远程计算机,并且它将愉快地查询每一个。

PS C:\> Get-CimInstance -ClassName Win32_UserProfile -ComputerName localhost,WINSRV

删除用户配置文件

一旦您了解了如何在计算机上列举用户配置文件,您可以进一步删除这些用户配置文件。

I can’t count how many times I’ve had to delete user profiles because something got corrupted and I just needed the user to log in again and recreate it. At one time, I would simply have the user log off and remove the C:\Users<UserName> folder from the file system. Usually it works, sometimes it didn’t. What I didn’t realize was that I was actually leaving some remnants behind.

这样做的正确方式是通过 CIM 发起删除。

使用您刚刚查看的相同 CIM 类,不仅可以查看配置文件,还可以完全删除它们。这与进入系统设置下的用户配置文件框并点击删除按钮相同。

User Profiles

为此,再次枚举用户配置文件,并此次应用过滤器以选择要删除的单个用户配置文件。在这种情况下,删除名为UserA的用户配置文件。您可以使用 PowerShell 的Where-Object cmdlet 和一些字符串操作来从LocalPath属性中提取用户文件夹名称,如下所示。

一旦您能够缩小到单个配置文件,您可以将该 CIM 实例传递给 Remove-CimInstance cmdlet,以处理 Get-CimInstance 返回的每个对象。此过程将从文件系统和注册表中删除用户配置文件。

Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } | Remove-CimInstance

同样,如果您想要扩展到多台计算机,只需在 Get-CimInstance 上使用 ComputerName 参数即可。

Get-CimInstance -ComputerName SRV1,SRV2,SRV3 -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } | Remove-CimInstance

使用 ManageEngine ADManager Plus 管理和报告 Active Directory、Exchange 和 Microsoft 365。下载免费试用版!

摘要

您现在已经看到了一个简单的枚举和删除 Windows 用户配置文件的方法。如果您不知道 CIM 类 Win32_UserProfile,您可能一直将 C:\Users\ 文件夹与配置文件关联,但现在您应该知道如何删除 Win32_UserProfile CIM 实例。

您可以看到用户配置文件远不止是一个简单的文件系统文件夹。下次需要从您的环境中的 Windows 计算机查询或删除用户配置文件时,请使用 CIM。

Source:
https://adamtheautomator.com/powershell-delete-user-profile/