https://stackoverflow.com/a/28741328/24355600
Here's, IMO, an ingenius way of implementing such cleanup. Start your script (I'm using after foreach-object start) with finding all variables, variabling them and clear everything but in the end.
Start:
$sysvars = get-variable | Select-Object -ExpandProperty name
$sysvars += 'sysvars'
End:
Get-Variable * | Where-Object { $_.name -notin $sysvars } | Remove-Variable