Search code examples
powershellregeditregistrykey

How to filter ProfileList with wildcards


I'm writing a script to help streamline the deletion of users profile list's on our Citrix servers. I cant figure out how to filter the ProfileImagePath by wildcards.

On our Windows 2008 servers in Regedit I can search and sort the profile list of users cia its Profileimagepath but this gets all the users, I cant seem to extend that filter to only bring back wildcard entries.

set-location | 'HKLM:\SOFTWARE\Microsoft\Windows 
NT\CurrentVersion\ProfileList' |% {Get-ItemProperty $_.pspath } | Select 
profileImagePath=username*

What I am getting is the whole list of user profiles, or if I put the filter =username* entry I get an error, What I wanted to get was just the results of the wildcard, Ideally just the usernames I put in the wildcard are what I want returned.


Solution

  • The code you posted is utterly broken. Please go find a PowerShell tutorial before proceeding any further.

    • Set-Location does not produce output, and even if it did, piping cmdlet output into a string wouldn't do anything at all.
    • You must not wrap statements at arbitrary places like you do in your sample code.
      • The wrapped registry path string will preserve the embedded newline (thus not matching what you want it to match).
      • Putting the argument of the Select-Object statement on the next line without escaping the newline will run Select-Object without arguments and the arguments as a separate statement (which most likely will throw an error because the arguments aren't a valid statement by themselves).
    • = is an assignment operator in PowerShell. Comparison operators are -eq (equality), -like (wildcard matches), and -match (regular expression matches).
    • You need Where-Object for filtering pipelined objects, not Select-Object. The latter is for selecting object properties.

    You propbably meant to do something like this:

    $username    = '...'
    $profilePath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
    
    Get-ChildItem $profilePath | Where-Object {
        (Get-ItemProperty $_.PSPath).ProfileImagePath -like "*${username}*"
    }
    

    or (simpler) like this:

    $username    = '...'
    $profilePath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
    
    Get-ChildItem $profilePath |
        Get-ItemProperty |
        Where-Object { $_.ProfileImagePath -like "*${username}*" }