~/2013/10/14/script-list-all-vms-with-datastore.md
PowerShell: List All VMs with Datastore
--- author: Tom Lasswell date: updated: read: 1 min in: [ps, scripts] tags: [powershell, vmware] ---
$ grep -n '^#' post.md
Script to list all the VMs that exist within your vCenter Server with their datastore.
Requirements
- Windows PowerShell with VMware vSphere PowerCLI installed. The script loads the
VMware.VimAutomation.Coresnap-in, so it needs a PowerCLI release that ships it (5.x era). - A vCenter account that can read datacenters, datastores and VMs.
Connect-VIServerprompts for credentials. - Network access to the vCenter Server.
- A
D:\scriptsfolder, or change the export path.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
vcServer | String | Yes | Name or address of the vCenter Server. The script exits with a message if it is missing. |
D:\scripts\datastore-output.csv | Path | No | Edit in the script: where the CSV is written. |
Usage
Pass the vCenter Server name and sign in when prompted.
.\Get-VMDatastore.ps1 -vcServer vcenter.example.com
The results are written to D:\scripts\datastore-output.csv, one row per VM and datastore, with Name, DataStore, VMHost, PowerState, Version and Folder columns.
Script
<#
.SYNOPSIS
Lists all VMs in a vCenter Server together with their datastore.
.DESCRIPTION
Connects to the vCenter Server, walks every datastore in every datacenter and collects the VMs
stored on it (name, datastore, host, power state, hardware version and folder). The result is
written to D:\scripts\datastore-output.csv and the session is disconnected.
.PARAMETER vcServer
Name or address of the vCenter Server. The script exits with a message if it is missing.
.EXAMPLE
.\Get-VMDatastore.ps1 -vcServer vcenter.example.com
.NOTES
Author : Thomas Lasswell (https://www.techcolumnist.com)
Version : 1.1 (2026-09-20)
History : 1.0 2013-10-14 Initial version.
1.1 2026-09-20 Disconnects without a confirmation prompt.
Requires: VMware PowerCLI with the VMware.VimAutomation.Core snap-in
#>
param(
[string]$vcServer
)
Clear-Host
if (!$vcServer) {
Write-Host "No vCenter Server Specified"
exit
}
Add-PSSnapin VMware.VimAutomation.Core
$host.UI.RawUI.WindowTitle = "PowerShell [PowerCLI Module Loaded]"
Connect-VIServer $vcServer
$aDatastoreVM = Get-Datacenter | Get-Datastore | ForEach-Object {
$ds = $_.Name
$_ | Get-VM | Select-Object Name, @{n = 'DataStore'; e = { $ds } }, VMHost, PowerState, Version, Folder
}
$aDatastoreVM | Export-Csv "D:\scripts\datastore-output.csv" -NoTypeInformation -UseCulture
Disconnect-VIServer $vcServer -Confirm:$false
Notes
- Update 2026-09-20 (v1.1):
Disconnect-VIServernow uses-Confirm:$false, so an unattended run no longer stops at a confirmation prompt.