如何仅为特定的少数几个用户获取$ userPath

时间:2019-07-05 06:51:59

标签: powershell user-profile

使用下面的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个用户(例如user1user2)对此进行过滤?

2 个答案:

答案 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
#>