PowerShell Where-Object $_.name -like -in $list

I am new to PowerShell and ran into a bit of a roadblock. I am trying to pull program name and version information from multiple servers.

I have a list of the program names in a $list variable, but the program names also contain the version numbers in them. I am just storing the names of the programs in the list variable without the version numbers.

I am trying to figure out a way to use both the -like and -in parameters with the Where-Object cmdlet in order to match the full program entry name (e.g. AdToUserCacheSync 1.10.1.10) with my entry in the $list variable (e.g. AdToUserCacheSync).

How can I do this?

$list = Get-Content "\\server\c$\temp\list.txt" $storeTestServers = Get-Content "\\server\c$\temp\testStores.txt" foreach ($server in $storeTestServers) { Get-WmiObject -Class Win32_Product -ComputerName $server | Select-Object -Property PSComputerName, Name, Version | Where-Object {$_.pscomputername -like "940*" -and $_.name -like -in "*$list*"} } 

2 Answers

The Where-Object FilterScript block is just a scriptblock that returns $true, $false or nothing - you can do all kinds of crazy things inside it, including looping over an array to see if there is a wildcard match in one of the entries:

Where-Object { $ProductName = $_.Name $_.pscomputername -like "940*" -and ( $list | ForEach-Object { if($ProductName -like "*$_*"){ return $true } } ) } 

I found the Adobe version by PowerShell:

Get-WmiObject -Class Win32_Product -ComputerName 127.0.0.1 | Select-Object -Property PSComputerName, Name, Version | Where-Object {$_.Name -like "Adobe*"} | Out-File Adobe_Log.log 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like