VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Get all VMs not in the INFRA folder

Hi guys,

I would like to select all VMs in my environment that DO NOT reside in the INFRA folder.  I'm not having much luck doing it on my own, I would imagine it's something like:

get-folder | select -name isnot "INFRA" | get-vm

Maybe I'm way off, I'm not having much luck finding an example close to what I'm trying to achieve on my own.  Could anyone give me a hand?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Found it, you have to add the NoRecursion switch

Get-Folder | where{$_.Name -notmatch 'INFRA'} | Get-VM -NoRecursion | Select Name,Folder


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I would go for a Wher-clause with a notmatch on a RegEx expression.

Something like this

Get-Folder | where{$_.Name -notmatch 'INFRA'} | Get-VM

And if you want to exclude only INFRA and not for example INFRA1, you could do

Get-Folder | where{$_.Name -notmatch '^INFRA$'} | Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Thanks for the help LucD!

0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Spoke too soon, looks like it's still returning VMs from the INFRA folder:

Untitled.jpg

It almost looks like a glitch of some kind because if I use match I only get results in INFRA which I would expect, but notmatch returns everything including INFRA:Untitled.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try with the exact notmatch option I gave earlier.

Does that make a difference?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Found it, you have to add the NoRecursion switch

Get-Folder | where{$_.Name -notmatch 'INFRA'} | Get-VM -NoRecursion | Select Name,Folder


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Untitled.jpg

That got it, thanks again LucD!

0 Kudos