Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 632
Jerry Bust Avatar asked Mar 18 '23 04:03

Jerry Bust


2 Answers

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

Get-VM | Where-Object { $_.Name -notlike '*Web1*' -and $_.Name -notlike '*Web2*'}
like image 194
dotnetom Avatar answered Mar 27 '23 18:03

dotnetom


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

like image 26
FoxDeploy Avatar answered Mar 27 '23 19:03

FoxDeploy