How to Filter Select results?

Welcome Forums General PowerShell Q&A How to Filter Select results?

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #108001

    Participant
    Points: 1
    Rank: Member

    Hello, I'm a PS beginner!  I'm trying to create a report and I want to filter out the results so I only get back certain companies (i.e. Company like "*Paul*").  When I try adding a -Filter to the Get-AdUser statement, it errors out  with

    Get-ADUser : Parameter set cannot be resolved using the specified named parameters.
    At C:\Work\test.ps1:10 char:34
    + ... tNames | % {Get-ADUser -Filter {Company -like "*Paul*"} -Identity $_.s ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    $Details = $sAMAccountNames | % {Get-ADUser -Filter {Company -like "*Paul*"} -Identity $_.sAMAccountName -Properties DisplayName, LastlogonDate, Enabled, AccountLockoutTime, LastBadPasswordAttempt, BadPwdCount, LockedOut, Company, Description}

    Here is my working code, but without the filter.

    $LockedRpt = "c:\work\LockedAccounts.csv"
    # delete the existing output file
    $exists = Test-Path $LockedRpt
    If ($exists -like "True")
    {
    Remove-Item $LockedRpt
    }
    Import-module ActiveDirectory -ErrorAction stop
    $sAMAccountNames = Search-ADAccount -UsersOnly -LockedOut | Select SamAccountName
    $Details = $sAMAccountNames | % {Get-ADUser -Identity $_.sAMAccountName -Properties DisplayName, LastlogonDate, Enabled, AccountLockoutTime, LastBadPasswordAttempt, BadPwdCount, LockedOut, Company, Description}
    $Details | Select SamAccountName, DisplayName, LastlogonDate, Enabled, AccountLockoutTime, LastBadPasswordAttempt, BadPwdCount, LockedOut, Company, Description | Export-Csv c:\work\LockedAccounts.csv -NoTypeInformation

    Thanks for any help!

  • #108004

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    You can't use both -Identity and -Filter at the same time. The online docs (https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps) have multiple "blocks" or parameter sets, and you can't mix and match parameters between them.

  • #108008

    Participant
    Points: 1
    Rank: Member

    Is there a way to filter the results after the fact?  On the select

    $Details | Select SamAccountName, DisplayName, LastlogonDate, Enabled, AccountLockoutTime, LastBadPasswordAttempt, BadPwdCount, LockedOut, Company, Description | Export-Csv c:\work\LockedAccounts.csv -NoTypeInformation

  • #108011

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    You'd use Where-Object, not Select-Object.

  • #108014

    Participant
    Points: 160
    Helping Hand
    Rank: Participant

    One of the fundamental things about learning Powershell is the pipeline and how it works. The pipeline allows you to pass objects to another command.  If you run the following command to get help:

    Get-Help Get-ADUser -Full

    You'll see that -Identity accepts an ADUser and more importantly, pipeline input:

        -Identity 
            
            Required?                    true
            Position?                    0
            Accept pipeline input?       true (ByValue)
            Parameter set name           Identity
            Aliases                      None
            Dynamic?                     true
    

    This basically means that if you pipe ADUser objects to Get-ADUser, it will automatically do a for loop for you. You'll learn more about this when you start writing functions. This means that we can pipe directly from Search-ADAccount to Get-ADUser. Take a look at the modified script below:

    $LockedRpt = "c:\work\LockedAccounts.csv"
    
    If (Test-Path -Path $LockedRpt){
        Remove-Item -Path $LockedRpt -Force
    }
    
    Import-module ActiveDirectory -ErrorAction stop
    
    $Details = Search-ADAccount -UsersOnly -LockedOut | 
               Get-ADUser -Properties DisplayName, 
                                      LastlogonDate, 
                                      Enabled, 
                                      AccountLockoutTime, 
                                      LastBadPasswordAttempt, 
                                      BadPwdCount, 
                                      LockedOut, 
                                      Company, 
                                      Description
    
    $Details | Export-Csv -Path $LockedRpt -NoTypeInformation
    
  • #108020

    Participant
    Points: 1
    Rank: Member

    Very cool.  And then I was able to pipe again into Where-Object and only pull out what I needed.  Thank you!

     

    $LockedRpt = "c:\work\LockedAccounts.csv"
    # delete the existing output file
    $exists = Test-Path $LockedRpt
    If ($exists -like "True")
    {
    Remove-Item $LockedRpt
    }
    Import-module ActiveDirectory -ErrorAction stop
    
    $Details = Search-ADAccount -UsersOnly -LockedOut |
    Get-ADUser -Properties DisplayName,
    LastlogonDate,
    Enabled,
    AccountLockoutTime,
    LastBadPasswordAttempt,
    BadPwdCount,
    LockedOut,
    Company,
    Description | Where-Object {$_.Company -like "*Paul*" -or $_.Description -like "*Paul*" }
    
    $Details | Export-Csv -Path $LockedRpt -NoTypeInformation
    
    

The topic ‘How to Filter Select results?’ is closed to new replies.