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システム管理者は以下の問題に対処しなければなりません:

  • ユーザーレジストリハイブの破損
  • すべてのユーザープロファイルで共有する必要があるファイル
  • 破損したプロファイルを再作成する方法を見つける
  • …など

かつてはイライラする体験でしたが、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コマンドレットは、ローカルだけでなくリモートでも動作します。 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コマンドレットとLocalPathプロパティからユーザーフォルダ名を取得するための文字列操作を使用して行うことができます。

Get-CimInstanceが返す各オブジェクトに対して、その単一のプロファイルを絞り込むことができたら、そのCIMインスタンスをRemove-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ユーザープロファイルを列挙して削除する簡単な方法を見てきました。CIMクラスWin32_UserProfileを知らなかった場合、プロファイルとしてC:\Users<Username>フォルダを関連付けていたかもしれませんが、Win32_UserProfile CIMインスタンスを削除する必要があることを知っておくべきです。

ユーザープロファイルは、単純なファイルシステムフォルダ以上のものであることがわかりました。Windowsコンピューターの環境でユーザープロファイルをクエリまたは削除する必要がある場合は、CIMを使用してください。

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