Search code examples
powershelluser-profile

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?


Solution

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