Multi Server Management for Hyper-V (Part 4)

If you would like to read the other parts in this article series please go to:


I ended the previous article in this series by demonstrating a technique for reading a list of virtual machines from a text file and then acquiring information about those virtual machines. This technique can be extremely useful because it gives you an easy way of categorizing your virtual machines. For example, if you were interested in periodically verifying the health of your Exchange Servers, you might create a text file listing all of your Exchange Servers and then build a PowerShell script that examines this file. Likewise, you might create a text file for your domain controllers or for your SharePoint servers.

Regardless of how you plan to use text files containing lists of virtual machines, you have to create the text files before you can use them. Building the text files isn’t a big deal if you only have a few virtual machines, but the process can become extremely tedious and time consuming for organizations that have large numbers of virtual machines. The creation of text files can also be problematic for organizations that are frequently creating new virtual machines.

If you want to use text files for virtual machine management, then you can avoid some of the complexities of text file creation by automating the process. You can actually use PowerShell cmdlets to create text files that you can later use for virtual machine management. In this article, I will show you how it’s done.

Basic Text File Creation

There are a few different ways that you can create a basic text file in PowerShell. The cmdlet that tends to be used for this purpose most often is Set-Content. Suppose for example that you wanted to add the phrase Hello World to a text file named TEST.TXT. You could do so by using the following PowerShell command:

Set-Content –Value “Hello World” –Path C:\Temp\Test.txt

Although this is probably the most popular cmdlet for creating a text file, I am going to be using a different cmdlet in this article. The cmdlet that I will be using is Out-File. Both cmdlets are designed to create text files, but since we are going to be populating the text file with virtual machine names, using the Out-File cmdlet will result in a little bit less coding. With that said, if you wanted to create a text file named TEST.TXT containing the phrase Hello World using the Out-File cmdlet, the command for doing so would be:

“Hello World” | Out-File –FilePath C:\Temp\Test.txt

Adding Virtual Machine Names to the Text File

So far I have shown you some techniques for adding raw text to a text file, but that isn’t what this article is all about. My goal in writing this article is to show you how to automate the text file creation process. That being the case, we will need to write PowerShell code that retrieves the names of the virtual machines and then adds those virtual machine names to a text file.

The easiest way to accomplish this goal is to use the Get-VM cmdlet to retrieve the names of the virtual machines and then use the Out-File cmdlet to add those virtual machine names to a text file. Suppose for instance that you wanted to create a text file named My-VMs.txt that contained the names of all of the virtual machines residing on the host server. The command for doing so would be:

Get-VM | Out-File –FilePath C:\Temp\My-VMs.txt

You can see the command and its output in Figure A.

Figure A: We have created a text file containing the names of our virtual machines.

As you can see in the figure above, the text file creation process was successful, but we have a little bit more work to do. The cmdlet included not just the names of the virtual machines, but also information such as the VM’s state and memory usage. This information is useful if we wanted to create a VM resource usage report, but it won’t work for our purposes. The text file needs to contain virtual machine names and nothing else. Fortunately, this is very easy to accomplish. The command for doing so is:

Get-VM | Format-Table Name –HideTableHeaders | Out-File –FilePath C:\Temp\My-VMs.txt

The command above works in the same basic way as the previous command, but I have made two changes. First, I have added the command Format-Table Name. This causes the Get-VM cmdlet’s output to hide everything except for the virtual machine names. However, the column name is still displayed above the list of virtual machines. To get rid of the column name so that we just have a raw list of virtual machine names, we must append the –HideTableHeaders parameter.

Populating the Text File

Earlier I showed you a method for adding the names of all of the virtual machines on a given host to a text file. However, it might often be more convenient to create text files containing the names of virtual machines that meet certain criteria. For example, you might want to build a text file containing the names of all of your virtualized domain controllers.

To do so, you can use the same basic text file creation technique that I demonstrated earlier. However, the command needs to be extended a bit so that we can filter the list of virtual machine names.

To show you how such a command might work, imagine that you wanted to create a text file containing all of your virtualized domain controllers. In such a situation, naming conventions might be useful. For example, my lab server contains two virtualized domain controllers (Lab15DC and Lab15DC2). Because both domain controllers have similar names, I could use the naming format to compile the list.

To show you what I mean, take a look at the following command:

Get-VM –Name Lab15DC*

This command returns the names of all of the virtual machines whose names start with Lab15DC, as shown in Figure B.

Figure B: The Get-VM cmdlet can return specific virtual machine names.

We can easily incorporate this technique into the command that was used earlier to create a text file. The full command would look like this:

Get-VM –Name Lab15DC* | Format-Table Name –HideTableHeaders | Out-File –FilePath C:\Temp\DCs.txt

To see what this command looks like in action, take a look at Figure C. In this figure, I have used the command listed above to create a text file named DCs.txt. I then used the Get-Content cmdlet to retrieve and display the contents of the file that was created. As you can see in the figure, the text file contains only the names of my virtualized domain controllers.

Figure C: We can automatically create a text file containing the names of the virtualized domain controllers.

Obviously creating a list of virtual machine names and then displaying those names isn’t very useful by itself. However, as you saw in the previous article, the list can be used to automate the virtual machine management process. You can use this technique to develop scripts that can be run against multiple virtual machines.


Hyper-V 3.0 makes it easy to perform operations against remote virtual machines or against remote host servers. You can even automate management actions by scripting them through PowerShell.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top