Register a LUN full of VMs with PowerCLI

One of the great things about VMware vSphere is virtual machine encapsulation — a VM is simply a directory of files that can be easily copied around by administrators for a variety of purposes.  Most other platforms obfuscate virtual machines beyond recognition.

Recently, I was working on a project that required a large number of VMs that I had used previously but were no longer registered on a VMware ESXi host — they were just waiting patiently on a SAN LUN for their day to come.

Using the vSphere Client to connect the VMFS datastore to a host was a snap.  However, I was not enthusiastic about the several dozen clicks that would have been needed to bring them all back into the inventory for use, so I came up with a simple PowerCLI command to do all the work.

I was so pleased with the power of PowerCLI that I had to share…

Register all virtual machines on a datastore

Connect to vCenter Server with a PowerCLI session and adjust the obvious placeholders to suit your environment:

A few moments later, all the VMs are correctly registered and ready for action.

Build a local hosts file with PowerCLI

Another issue I encountered was the lack of DNS for guest operating systems at an offsite lab.  There was no way to connect to the various VMs by name, only IP address — not my favorite approach.  Since vSphere has information about guest networking, I quickly generated a list of IP addresses and VM names in a format suitable for pasting into my local hosts file:

PowerCLI to the rescue again.  What are your favorite PowerCLI one-liners?

(Visited 7,638 times, 1 visits today)

Tags: , ,


  1. Alan Renouf’s avatar

    Love the local host file trick, thats cool !

    1. Eric Gray’s avatar

      Thanks, Alan. Nice to hear from you.

    2. Vladan’s avatar

      I like those one liners… Bookmark now. -:) Excellent.

      1. Eric Gray’s avatar

        Thanks, Vladan. Got any PowerCLI tricks to share?

      2. Erick Moore’s avatar

        Just to note, on PowerCLI 4.1, “New-VM -Host” will need to be “New-VM -VMHost”. Very slick!

      3. LucD’s avatar

        Two great and very useful PowerCLI scripts.
        You can find another method to register guests and templates in my post.

        1. Eric Gray’s avatar

          Thanks, Luc. That is quite an awesome script, very flexible.

        2. Tony’s avatar

          Nice article Eric! I recently did a DR test with VM’s sitting on NetApp NFS replicating to another NetApp at our DR site…Of course VMware shined and the test went flawlessly. I’d like to start scripting the mounting of NFS exports and the registering of VM’s. This script is definitely going into my bag o’ tricks. I love the new SRM, but I’m trying to do things on the cheap.

          Thanks again!

          1. Eric Gray’s avatar

            Excellent, thanks for sharing your success.

          2. Susan Roter’s avatar

            Worked like a charm. You have a new follower Eric. Thank you.

          3. Sami’s avatar

            What is this ‘vmstores:\’ ??? this is new to me, what should I be googling for ??

          4. Sami’s avatar

            Never mind, I got it now 🙂

            Using ‘Get-VMGuest’ assumes that all vms have VMware-Tool installed, no??

            Great article!!

Comments are now closed.