使用下面的poweshell
脚本,我可以看到所有用户个人资料的userpath
。
# Get a list of all user profiles
$users = Get-WmiObject Win32_UserProfile
foreach( $user in $users ) {
# Normalize profile name.
$userPath = (Split-Path $user.LocalPath -Leaf).ToLower()
Write-Host $userPath
}
如何使用特定的2个用户(例如user1
和user2
)对此进行过滤?
答案 0 :(得分:2)
您可以使用参数filter
和WQL来查询WMI返回的结果。
尝试使用此代码
$users = Get-WmiObject win32_userprofile -filter 'LocalPath LIKE "%user1%" OR LocalPath LIKE "%user2%"'
foreach( $user in $users ) {
# Normalize profile name.
$userPath = (Split-Path $user.LocalPath -Leaf).ToLower()
Write-Host $userPath
}
请参见MS documentation for Get-WmiObject并查找Filter
参数
答案 1 :(得分:2)
你是说这个...
# Get a list of all user profiles
$users = Get-WmiObject Win32_UserProfile
foreach( $user in $users )
{ (Split-Path $user.LocalPath -Leaf).ToLower() }
# Results
<#
...
networkservice
localservice
systemprofile
#>
# Get a list of specific user profiles
foreach( $user in $users )
{ (Split-Path $user.LocalPath -Leaf).ToLower() | Select-String 'networkservice|localservice' }
# Results
<#
networkservice
localservice
#>
或者单线
(Split-Path (Get-WmiObject Win32_UserProfile |
Where-Object -Property LocalPath -match 'networkservice|localservice').LocalPath -Leaf).ToLower()
# Results
<#
networkservice
localservice
#>