파워셀 & 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 시스템 관리자는 다음과 같은 문제를 해결해야 합니다:

  • 손상된 사용자 레지스트리 하이브
  • 모든 사용자 프로필에서 공유해야 하는 파일
  • 손상된 프로필을 다시 생성하는 방법
  • … 그 외에도

일전에는 괴로운 경험이었지만, PowerShell을 사용하면 조금 덜 괴롭게 할 수 있습니다. PowerShell을 사용하면 Windows 사용자 프로필을 관리하는 것이 더욱 쉬워집니다.

ManageEngine ADManager Plus로 Active Directory, Exchange 및 Microsoft 365을 관리하고 보고하세요. 무료 평가판 다운로드!

사용자 프로필 나열하기

단일 Windows 컴퓨터의 파일 시스템에서 사용자 프로필을 확인하는 것은 쉽습니다. 단순히 C:\Users 폴더를 확인하면 됩니다. 그러나 이렇게 하면 전체 상황을 파악할 수 없을 뿐만 아니라 잠재적인 파일 시스템 접근 문제로 인해 문제가 될 수 있습니다. 더 좋은 방법은 WMI 또는 CIM을 사용하는 것입니다. CIM에서는 Win32_UserProfile이라는 클래스가 존재합니다. 이 클래스에는 컴퓨터에 존재하는 모든 프로필과 간단한 파일 시스템 폴더에서 알 수 없는 유용한 정보가 포함되어 있습니다.

PowerShell을 사용하면 Get-CimInstance 명령을 사용하여 이 CIM 클래스에 액세스할 수 있습니다.

아래에서는 로컬 컴퓨터에서 첫 번째 사용자 프로필을 찾습니다. LastUseTime, SID 등과 같은 많은 유용한 정보가 있습니다.

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-CimInstanceComputerName 매개변수를 사용하면 됩니다.

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 사용자 프로필을 열거하고 삭제하는 간단한 방법을 보았습니다. Win32_UserProfile CIM 클래스를 알지 못했다면 C:\Users<사용자이름> 폴더를 프로필로 오해하고 있었을 수도 있지만, 이제는 Win32_UserProfile CIM 인스턴스를 삭제할 수 있다는 것을 알아야 합니다.

단순한 파일 시스템 폴더보다 사용자 프로필에는 더 많은 내용이 있다는 것을 알 수 있습니다. Windows 컴퓨터에서 환경에 따라 사용자 프로필을 쿼리하거나 삭제해야 할 때는 CIM을 사용하세요.

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