Skip to content

Remove-Datacenter

VMware: Removes the specified datacenter

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core

[CmdletBinding(DefaultParameterSetName = "byName")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [string]$DatacenterName,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [string]$DatacenterID
)

Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop

        if ($PSCmdlet.ParameterSetName -eq "byID") {
            $dCenter = Get-Datacenter -Server $vmServer -Id $DatacenterID -ErrorAction Stop
        }
        else {
            $dCenter = Get-Datacenter -Server $vmServer -Name $DatacenterName -ErrorAction Stop
        }
        $dcName = $dCenter.Name
        Remove-Datacenter -Datacenter $dCenter -Server $vmServer -Confirm:$false -ErrorAction Stop

        $output = [PSCustomObject]@{
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            Status    = "Success"
            Message   = "Datacenter $dcName removed"
        }
        Write-Output $output
    }
    catch {
        throw
    }
    finally {
        if ($null -ne $vmServer) {
            Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue
        }
    }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Name of the datacenter to remove

ID of the datacenter to remove

An interactive directory of PowerShell scripts.