John, welcome to 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!.
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 |
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.