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
Pipe your output from Get-VM
to Where-Object
:
Get-VM | Where-Object { $_.Name -notlike '*Web1*' -and $_.Name -notlike '*Web2*'}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With