Skip to content

Remove-Folder

VMware: Removes the specified folder

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true)]
    [string]$FolderName,
    [switch]$DeletePermanently
)
Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $folder = Get-Folder -Server $vmServer -Name $FolderName -ErrorAction Stop
        if ($null -eq $folder) { throw "Folder $FolderName not found" }
        Remove-Folder -Server $vmServer -Folder $folder -DeletePermanently:$DeletePermanently -Confirm:$false -ErrorAction Stop
        [PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; Status = "Success"; Message = "Folder $FolderName successfully removed" }
    }
    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 folder to remove

Off

Delete from disk any VMs contained in the folder

An interactive directory of PowerShell scripts.