无法获取用户配置文件列表

Having trouble getting a list of users profiles

向最棒的社区致意,

我正在尝试编写一个脚本来列出电脑上的所有配置文件。 我想要的列表示例:

Admin 
JSmith 
LGale 
Mthinker

有没有人知道如何尝试获取信息或拥有可以共享的代码片段?

我在网上查看并做了一些研究,还查看了很多可用的脚本,但没有什么是我需要的。

您需要为 Win32_UserProfile 个实例查询 WMI - 每个配置文件都将链接到拥有该配置文件的用户的安全标识符,该标识符又可以转换为具有用户名的帐户对象:

Get-CimInstance win32_userprofile |ForEach-Object {
  # Convert SID string to SecurityIdentifier object
  $SID = $_.SID -as [System.Security.Principal.SecurityIdentifier]

  try {
    # Now we can resolve the actual account
    $SID.Translate([System.Security.Principal.NTAccount]).Value
  }
  catch {
    Write-Warning "Unable to translate SID '$($_.SID)' for profile at '$($_.LocalPath)' to account name"
  }
}

在我的机器上(只有一个本地帐户)列出:

COMPUTER-NAME\mathias
NT AUTHORITY\NETWORK SERVICE
NT AUTHORITY\LOCAL SERVICE
NT AUTHORITY\SYSTEM