VMware Cloud Community
Cyberfed27
Hot Shot
Hot Shot
Jump to solution

Easy one - dumping VM names to a text file

Hey folks, I am trying to write a script, one of the parts of it needs to get the names of all the VM's and write them to a text file.

However, I want nothing but the server names in the text file.

Here is what I have tried:

Get-VM | Select-Object Name | Out-File "C:\servers.txt"

This yields a text file as follows:


Name                                                                                                                                                                                                                                      
----                                                                                                                                                                                                                                      
SERVER1


SERVER 2

....and so on. Close, but the empty line between server names and the "Name ----"  lines need to go.

Next I tried

$VMs = Get-VM | Select-Object Name

Add-Content - Path "C:\servers.txt" $VMs

This yields a text file as follows:

@{Name=SERVER1}
@{Name=SERVER2}

..and so on.. I dont want the @{Name=}  parts

So I'm close but I just want each server name on one line and no other text or empty lines. Example of what I want is:

SERVER1

SERVER2

SERVER3

ect...ect...thoughts on how I can strip out the other things?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try

Get-VM | Select-Object -ExpandProperty Name | Out-File "C:\servers.txt"



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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try

Get-VM | Select-Object -ExpandProperty Name | Out-File "C:\servers.txt"



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

Reply
0 Kudos
Cyberfed27
Hot Shot
Hot Shot
Jump to solution

Exactly what I needed!

Many thanks!

Reply
0 Kudos
TomHowarth
Leadership
Leadership
Jump to solution

Luc, you are a walking PoSH/PowerCLI reference book Smiley Happy

Tom Howarth VCP / VCAP / vExpert
VMware Communities User Moderator
Blog: http://www.planetvm.net
Contributing author on VMware vSphere and Virtual Infrastructure Security: Securing ESX and the Virtual Environment
Contributing author on VCP VMware Certified Professional on VSphere 4 Study Guide: Exam VCP-410
raffic_ncc
Enthusiast
Enthusiast
Jump to solution

Awesome...Single Line script.... Perfect gives which gives what i needed..

Mohammed Raffic

http://www.vmwarearena.com

Mohammed Raffic VCP4,VCP5,VCAP4-DCA,VCAP5-DCA,VCP-Cloud, MCSA.MCTS,CCA http://www.vmwarearena.com
Reply
0 Kudos