# Filter by computer name then find last Windows client update

**URL:** <https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623>\
**Category:** PowerShell Help\
**Created:** [July 2, 2020, 5:33am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623 "2020-07-02T05:33:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![notation](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@notation](https://forums.powershell.org/u/notation)\
**Post date:** [July 2, 2020, 5:33am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/1 "2020-07-02T05:33:35Z")

</div>

Hi!

I am very new to powershell so apologies if this question is noobish.

In a domain I am attempting to filter by computer name then show for each the last update for each filtered Windows 10 client.

The filtering part seems to function, but interogating each to get the last update is wrong.

```
Get-ADComputer -filter * | Where-Object {$_.Name -like "abcad*"} | select -ExpandProperty Name
foreach{Get-HotFix -ComputerName $_ | Sort-object InstalledOn -Descending | Select-object -first 1
}
```

Any assistance would be appreciated!

Thanks

John

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 2, 2020, 6:13am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/2 "2020-07-02T06:13:19Z")

</div>

John, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

When you post code, error messages, sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **PRE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

To reduce the stress you put on your DC with a query like this I urgently recommend using the **-SearchBase** parameter for **Get-ADUser**. It would also be faster when you use the **-Filter** parameter of **Get-ADUser** instead of filtering the results provided by it later with a **Where-Object**.

The actual mistake you did - you forgot to pipe the output of “select -ExpandProperty Name” to Foreach-Object. 😉

Something like this should run actually

```
$SearchBase = 'OU=Computers,OU=Departement,DC=contoso,DC=com'

Get-ADComputer -Filter "Name -like 'abcad*'" -SearchBase $SearchBase |
ForEach-Object {
    Get-HotFix -ComputerName $_.Name |
    Sort-Object -Property InstalledOn |
    Select-Object -Last 1
}
```

… untested. I don’t have an environment to test at the moment.

And just for the sake of completeness - all computers you want to query that way have to be switched on and reachable on the network.

---

<div class="post-metadata">

**Author:** ![notation](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@notation](https://forums.powershell.org/u/notation)\
**Post date:** [July 5, 2020, 11:30am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/3 "2020-07-05T11:30:50Z")

</div>

Many Thanks Olaf!

I will follow your advice and aim to get it working in the coming week. I will report back what happens.

&nbsp;

Thanks again for your time!

---

<div class="post-metadata">

**Author:** ![notation](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@notation](https://forums.powershell.org/u/notation)\
**Post date:** [July 10, 2020, 3:29am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/4 "2020-07-10T03:29:21Z")

</div>

Hi

Further to the earlier post, I’ve been attempting to filter by computer name then show for each the last update for each filtered Windows 10 client using this script:

[pre]

$SearchBase = “OU=PHS,OU=RSS,OU=Computers,DC=org,DC=uk”  
Get-ADComputer -Filter “Name -like ‘abcad\*’” | -SearchBase $SearchBase |  
ForEach-Object {  
Get-HotFix -ComputerName $\_.Name |  
Sort-Object -Property InstalledOn -Descending |  
Select-Object -first 1  
}  
[/pre]

&nbsp;

In response I am getting the following error message:

**-SearchBase : The term ‘-SearchBase’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and**  
**try again.**  
**At line:1 char:47**  
**+ Get-ADComputer -Filter “Name -like ‘abcad\*’” | -SearchBase $SearchBase |**  
**+ ~~~~~~~~~~~ **  
**+ CategoryInfo : ObjectNotFound: (-SearchBase:String) , CommandNotFoundException**  
**+ FullyQualifiedErrorId : CommandNotFoundException**

I would be grateful for any assistance.

Many thanks!

JF

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 10, 2020, 3:51am UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/5 "2020-07-10T03:51:42Z")

</div>

**-SearchBase** is parameter for the cmdlet [Get-ADUser](https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps). Please **always** read the **complete** help **including the examples** for the cmdlets you’re about to use to learn how to use them.

---

<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:29pm UTC](https://forums.powershell.org/t/filter-by-computer-name-then-find-last-windows-client-update/14623/6 "2024-05-16T20:29:16Z")

</div>


