使用 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).

Windows 系統管理員需要處理:

  • 損壞的使用者註冊表 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命令,用於每個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<Username>文件夾與配置文件關聯起來,但現在您應該知道如何刪除Win32_UserProfile CIM實例。

您可以看到用戶配置文件不僅僅是一個簡單的文件系統文件夾。下次在您的環境中需要查詢或刪除Windows計算機中的用戶配置文件時,請使用CIM。

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