Someone on Stack Overflow shared the below PowerShell code. I have the code working in the script I modified. I'd like to add the Get-Module | Remove-Module; Clear-Host
functions to it.
And would adding those actually help or have any ill effects after running the script?
I'm new on Stack Overflow and don't have the reputation to comment on the thread the code is found in. So I made a new question.
### Start of script (store list of existing variable names)
$ExistingVariables = Get-Variable | Select-Object -ExpandProperty Name
### End of script (remove new variables)
$NewVariables = Get-Variable | Select-Object -ExpandProperty Name | Where-Object {$ExistingVariables -notcontains $_ -and $_ -ne "ExistingVariables"}
if ($NewVariables)
{
Write-Host "Removing the following variables:`n`n$NewVariables"
Remove-Variable $NewVariables
}
else
{
Write-Host "No new variables to remove!"
}