How to use multiple filters at once with Get-View

Writing a Virtual Machine (VM) report the other day to gather a list of VMs that conform to a known naming standard.


$vmlist = Get-View -ViewType VirtualMachine -Property Name -Filter @{"Name"="[a-z1-9]ap1"} | Select Name

Then when I did a count to confirm I found that I had many VMs than I expected. Upon investigation I found that this actually returns all Templates as well. So then how do a filter the templates out. The following filter will do this.


-Filter @{"Config.Template"="false"}

How do I combine these? Using an -and inside and outside the filter block did not work.

Semi colon is the answer. So my query becomes.


$vmlist = Get-View -ViewType VirtualMachine -Property Name -Filter @{"Name"="[a-z1-9]ap1"; "Config.Template"="false"} | Select Name

The output of this command is an AND. Combing both of these filters.

3 Comments


  1. Is there any way to get OR version through get-view..
    ok; let me explain..
    I have a VM Name but i don’t know whether its a vCenter Name or DNS hostname of that VM.

    so how can i get that VM either of”properties “Name”and “Guest.HostName”

    $vmlist = Get-View -ViewType VirtualMachine -Property Name -Filter @{“Name”=”^MyVmName$” ??? “Guest.hostName”=”^MyVmName$”}


  2. Hi Sanshis.
    The short answer is no. This is from the help of Get-View

    -Filter <Hashtable>
    Specifies a hash of <name>-<value> pairs, where <name> represents the property value to test, and <value> represents a pattern the property must match. If more than one pair is present, all the patterns must match.

    As you can see the last part “…all the patterns must match”

    My home lab is totally broken at the moment so I am unable to test what I am about to suggest. Hopefully I have my syntax correct.

    $vmlist = Get-View -ViewType VirtualMachine -Property Name, Guest.HostName | Where-Object -FilterScript {$_.Name -match "MyVmName" -or $_.Guest.hostName -match "MyVmName"} | Select Name, @{Name="HostName";Expression={$_.Guest.Hostname}}

    You may also have to play around with the -eq -match -like operators.

    I hope that helps and let us know how you go.


  3. Thanks Herschelle42, the -or workaround you replied with definitely works, although it’s a lot slower of course. Good stuff!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.