VMware Cloud Community
racerzer0
Contributor
Contributor

Help creating vm folders from CSV with VI toolkit

I have a list of folder names in a CSV that I want to use to create vm folders in vCenter. I'm fairly new to powershell.. so here is what I have so far:

$fldr=Import-Csv c:\Scripts\vmgroups2.csv | Select-Object Field2

foreach ($item in $fldr)

Get-Folder vm | New-Folder -Name ?????

I am not sure what to put after -Name... since it typically requires the value to be in quotes.

Any ideas on how I can accomplish this?

Thanks!

0 Kudos
2 Replies
racerzer0
Contributor
Contributor

Ah.. i figured it out. used $item.Field2 after -Name.

Works great!

0 Kudos
LucD
Leadership
Leadership

You can do that this way


$fldr = Import-Csv c:\Scripts\vmgroups2.csv
foreach ($item in $fldr){
	Get-Folder vm | New-Folder -Name $item
}

The $fldr variable will be an array containing all the names that are in your CSV file.

The foreach loop will run through this array and each element of the array will be in the $item variable.

Each folder will be created under the hidden "vm" folder as a so-called "blue" folder in the Virtual Machines & Templates view.

Update: just noticed from your 2nd entry that you appear to have more than 1 field per line in the CSV.


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

0 Kudos