5

I am trying to run the Get-VM and filter out some VM's by name.

So for example Get-VM | -name isnotlike "Web1" and "Web2"

How would I do this?

or something like this? But this doesn't work

Get-VM -Name -notlike WEBIMAGE1,WEBIMAGE2
1
  • I assume you are using WMF 4.0 by the way you are referencing the Name property, but just so you know if you are still using WMF 3.0 or earlier the Name property is actually a GUID. The human-readable name in pre-WMF 3.0 on a VM object is VMElementName. Commented Feb 4, 2015 at 19:49

2 Answers 2

7

Pipe your output from Get-VM to Where-Object:

Get-VM | Where-Object { $_.Name -notlike '*Web1*' -and $_.Name -notlike '*Web2*'}
Sign up to request clarification or add additional context in comments.

Comments

2

There is actually a comparison operator for -like and -not like, so we can use that to accomplish this task. Keep in mind that -like uses a Wildcard search '*', so you need to use a query like -like "VM1*" to get back VM11, VM100 and so on.

Get-VM | Where {($_.Name -notlike "Web1*") -and ($_.Name -notlike "Web2*")}

So assuming we have VMs Web1, Web2, Web3, and Web4, this command will return Web3 and Web4.

If you want some more infomation and examples about comparison operators like -and -notlike and -like, check out the PowerShell help and run Help About_comparison_operators

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.