# How to Filter Select results?

**URL:** <https://forums.powershell.org/t/how-to-filter-select-results/11103>\
**Category:** PowerShell Help\
**Created:** [August 8, 2018, 2:31pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103 "2018-08-08T14:31:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpaulwass](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@mrpaulwass](https://forums.powershell.org/u/mrpaulwass)\
**Post date:** [August 8, 2018, 2:31pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/1 "2018-08-08T14:31:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 8, 2018, 3:01pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/2 "2018-08-08T15:01:09Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mrpaulwass](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@mrpaulwass](https://forums.powershell.org/u/mrpaulwass)\
**Post date:** [August 8, 2018, 3:13pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/3 "2018-08-08T15:13:45Z")

</div>

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_

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 8, 2018, 3:21pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/4 "2018-08-08T15:21:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 8, 2018, 6:44pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/5 "2018-08-08T18:44:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrpaulwass](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@mrpaulwass](https://forums.powershell.org/u/mrpaulwass)\
**Post date:** [August 9, 2018, 4:47am UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/6 "2018-08-09T04:47:48Z")

</div>

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

&nbsp;

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/how-to-filter-select-results/11103/7 "2024-05-16T20:34:50Z")

</div>


