PowerShell 技能连载 - 搜索本地用户

您知道吗?您可以搜索计算机上的本地用户,就像搜索域账户一样。

以下的示例代码搜索所有以“A”开头并且是启用状态的本地用户:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement

$type = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext('Machine', $env:COMPUTERNAME)

$UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($type)

# adjust your search criteria here:
$UserPrincipal.Name = 'A*'
# you can add even more:
$UserPrincipal.Enabled = $true

$searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
$searcher.QueryFilter = $UserPrincipal
$results = $searcher.FindAll();

$results | Select-Object -Property Name, LastLogon, Enabled

类似地,要查找所有设置了密码、密码永不过期,并且是启用状态的本地用户,试试以下代码:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement

$type = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext('Machine', $env:COMPUTERNAME)

$UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($type)

# adjust your search criteria here:
$UserPrincipal.PasswordNeverExpires = $true
$UserPrincipal.Enabled = $true

$searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
$searcher.QueryFilter = $UserPrincipal
$results = $searcher.FindAll();

$results | Select-Object -Property Name, LastLogon, Enabled, PasswordNeverExpires
作者

吴波

发布于

2013-12-23

更新于

2022-07-06

许可协议

评论