Highlighted
Enthusiast
Enthusiast

Find Lower Case VMs

Hi all,

Trying to find all the lowercase VMs so I can then rename to UPPERCASE.

When I run my command below it only returns a single lowercase VM when I know I have a lot more than one.

Can someone push me in the right direction please?

Thanks....

get-vm | Where-Object { $_.name -match "[A-Za-z]+_[A-Za-z]+" }

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
5 Replies
Highlighted
Enthusiast
Enthusiast

You must use cmatch command:

$vms = (Get-VM -Name TEST_vm).Name

if ($vms -cmatch '^[A-Z\0-9\s-]*$') {write-host "name has allowed chars"} else {Write-Warning "name has low chars"}


In this case allowed chars are A-Z, 0-9 and -


First line puts in $vms variable only VM name. You should probably change $vms variable to an array.

If all your VMs must be uppercase, you can also use ToUpper() method. Try this:

$vms.ToUpper()

0 Kudos
Highlighted
User Moderator
User Moderator

Try like this

Get-VM | where {$_.Name -cmatch "^[a-z]+_[a-z]+$"}


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

The only one I found to work was this...

get-vm | Where-Object { $_.name -cmatch "[a-z]+" }

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
Highlighted
User Moderator
User Moderator

What is the layout of your VM names then ?

Is it "abc-xyz" like your first code seems to suggest, or "abc" like this last code seems to suggest ?


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

0 Kudos
Highlighted
Hot Shot
Hot Shot

This should work.  I've used it in reverse to rename all upper to lower case.  I left the -Whatif there, but I usually change it to -confirm:$false

get-vm | where { $_.name.ToLower() -ceq $_.name } | foreach { Set-VM -VM $_ -Name ($_.name.ToUpper() ) -whatif }