Public/Remove-WindowsServerLabVM.ps1
function Remove-WindowsServerLabVM { [CmdletBinding()] param( [Parameter (Mandatory=$True)] [string]$Name, [switch]$RemoveDisk ) try { $VM = Get-VM $Name -ErrorAction SilentlyContinue Write-Verbose "Stopping and deleting existing VM '$Name'" Stop-VM $VM -Force -Passthru -WarningAction 'SilentlyContinue' | Remove-VM -Force if ($RemoveDisk) { Write-Verbose "Deleting the hard disk associated with VM '$Name'" Remove-Item -Path $VM.path -Force -Recurse } } catch { $PSCmdlet.ThrowTerminatingError($_) } } |