user584018
user584018

Reputation: 11304

how to fetch $userPath for only specific few users

With below poweshell script I'm able to see the userpath for all user profile.

# 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

}

How to filter this with specific 2 users, say user1 and user2?

Upvotes: 0

Views: 959

Answers (2)

postanote
postanote

Reputation: 16096

You mean this...

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

Or one-liner

 (Split-Path (Get-WmiObject Win32_UserProfile | 
 Where-Object -Property LocalPath -match  'networkservice|localservice').LocalPath -Leaf).ToLower()

# Results
<#
networkservice
localservice
#>

Upvotes: 2

Shadowfax
Shadowfax

Reputation: 586

You can filter the results returned from WMI by using the parameter filter and WQL for querying.

Try this code

$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
}

See MS documentation for Get-WmiObject and look for the Filter parameter

Upvotes: 2

Related Questions