Use this when automating Hyper-V or vSphere with PowerShell or PowerCLI for provisioning, reporting or bulk changes.
PowerShell
PowerShell automates Hyper-V with the Hyper-V module (New-VM, Set-VMProcessor, Checkpoint-VM) and vSphere with VMware PowerCLI (Connect-VIServer, New-VM, Get-Stat), covering bulk provisioning, reporting and changes.
1. PowerShell manages Hyper-V (Hyper-V module) and vSphere (PowerCLI).
2. Use remoting for many hosts.
3. Pipelines for bulk changes — test with -WhatIf.
4. Store credentials securely.
5. Log changes.
script → module → host/vCenter
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Get-Module Hyper-V -ListAvailableVerification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Checks Verify boundary
Get-Module Hyper-V -ListAvailable
Invoke-Command -ComputerName hv01 { Get-VM }
Run against several hosts with remoting; use -ErrorAction Stop and try/catch.
Step 2 Common mistakes Narrow the fault
- Running scripts locally only; use remoting for many hosts.
- No
-ErrorAction Stop, so failures continue silently.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Use -WhatIf before bulk changes.
- Objects, not text — filter by properties.
- PowerCLI needs Connect-VIServer per session.
Get-VM | ft Name,State,CPUUsage,MemoryAssigned— Hyper-V moduleConnect-VIServer <vc>; Get-VM— PowerCLIInvoke-Command -ComputerName <host> { Get-VM }— remote
ReferenceWhy it matters1 section · depth on demand
Remoting at scale
Invoke-Command -ComputerName hv01,hv02 -ScriptBlock { Get-VM | Where State -eq Running | Select Name,Uptime }
Documented example, not captured output.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
PowerShell for virtualization: Hyper-V module, Failover Clustering module, VMware PowerCLI (VCF.PowerCLI / VMware.PowerCLI).
# Hyper-V: report VMs with checkpoints older than 3 days
Get-VM -ComputerName hv01,hv02 | Get-VMSnapshot | ? CreationTime -lt (Get-Date).AddDays(-3) | Select VMName,Name,CreationTime
# PowerCLI: VMs with CPU ready > 5 % in last day
Connect-VIServer vcenter01
Get-VM | ForEach-Object { $r = Get-Stat -Entity $_ -Stat cpu.ready.summation -Start (Get-Date).AddDays(-1) -IntervalMins 30 | Measure-Object Value -Average; [pscustomobject]@{VM=$_.Name; ReadyPct=[math]::Round($r.Average/(1800*1000)*100/$_.NumCpu,1)} } | ? ReadyPct -gt 5
Examples
New-VM -Name vm01 -Generation 2 -MemoryStartupBytes 4GB -NewVHDPath D:\VMs\vm01.vhdx -NewVHDSizeBytes 60GB -SwitchName External
Set-VMProcessor vm01 -Count 2
Checkpoint-VM vm01 -SnapshotName pre-patch
Get-VM | Where State -eq Running
Documented example, not captured output.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Automation → PowerShell. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://libvirt.org/html/index.html · confidence 94%