There are two options if you want to write the output of the script to a .csv file. You can change the last line of the script into:
} | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
This will pipe the output of the script to the Export-CSV cmdlet. And it will create a .csv file called VmInfo.csv.
The second option is to change the first line of the script into:
$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
And append the following line at the end of the script:
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
This will put the output of the script from my previous post into a variable $VmInfo. In the last line the variable is used to output it to a .csv file.