Remove Variables on Script Completion

Hi,

Is there a way to remove variables that I create in a script after it has finished running. I have a script with a dozen odd variables which I would like to delete after the script has done its job.

The options I have seen are:

  • Close down the Powershell environment and restart
  • Use Remove-Variable command

If I use get-variable and limit the scope to script I see other variables which I did not define. I would only like to remove the variables which I have defined. 

Any suggestions would be appreciated. 

Regards,

MobileIAm

March 24th, 2015 10:28am

Hi,

I would either type the explicit list at the end of the script or prefix all variables that will be used with something specific so you can can then use that with a wildcard in Remove-Variable.

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 10:38am

If you only are defining script level variables then they will disappear after the scrip has completed.

If you just want to know which variables you have created then use Get-Variable and save the list.  Compare the current snapshot with the original and you will see the differences.

$begin=Get-Variable
... script
$end=Get-Variable

Compare-Object $begin $end

March 24th, 2015 11:57am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics