PowerShell: Delete Array, Clear Array

By Xah Lee. Date: . Last updated: .

Delete Array Variable by Remove-Variable

use Remove-Variable to remove the variable.

$x = @(1,2,3)
Remove-Variable x

Set Array value to $null

assign $null to the array.

this is an effective way to remove the array.

$x = 1,2,3
$x = $null
$x

Clear Array, Reset All Items

array.Clear() Sets all item values to the default value of the array's item type.

$x = 0, 1, 2, "some"
$x.Clear()

Write-Host ($x.length -eq 4)
# True

PowerShell: Array