Jerry Bust
Jerry Bust

Reputation: 85

powershell Get-VM where name is not like

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

Upvotes: 5

Views: 40558

Answers (2)

FoxDeploy
FoxDeploy

Reputation: 13537

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

Upvotes: 2

dotnetom
dotnetom

Reputation: 24901

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

Get-VM | Where-Object { $_.Name -notlike '*Web1*' -and $_.Name -notlike '*Web2*'}

Upvotes: 7

Related Questions