Link to home
Start Free TrialLog in
Avatar of KTN-IT
KTN-ITFlag for United States of America

asked on

Do I need to "clean up" variables/objects in Powershell?

I am just getting started in Powershell scripting.  I have experience in VB and am aware of the importance of "cleaning up" object variables (set objVar = Nothing) when I'm done with them to free up system memory.

My question is, do I need to do the same thing in my Powershell scripts?

For instance, for a line like:
$conn = new-object ('System.Data.SqlClient.SqlConnection')

At some point later should I "Remove-variable" this?
If so, do I only need to do this for object variables?

P.S. I can't believe EE doesn't have a "Powershell" zone.  It's still called "MSH/Monad" here.  They need to fix that.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KTN-IT

ASKER

Thanks, Chris.

I remember you helped me a while back with my server/AD issues, question #23638901.

Powershell is neat.  I can type gv and see all the variables, and remove-variable if I need to.

I'm trying to make a PSH script that runs on a schedule, so the session shouldn't last long at all.  Still, this is more of a question for my education than it is to solve a specific problem.  I want to be able to know how to clean up after myself, whether or not I actually need to.
Avatar of KTN-IT

ASKER

Even if 99% of the time it's clutter, that's important.  What if only 99% of the mail got delivered every day?  What it my code is only 99% correct?  "Close" counts in horseshoes and hand grenades, but not usually in computers...

Just my opinion  :^)

hehe I guess if I were an efficient programmer I would do it only where necessary. However, I'll apply the excuse that I'm a Sys Admin and not a programmer so absolute efficiency is something I'll leave behind.

If I have a really really big object in memory I might set it to $Null, or use Remove-Variable, when I'm done (typically if I'm trying to parse obscenely large log files). I've yet to need to call Garbage Collection explicitly and as the lifetime of the majority of my scripts is very short even that much is more than normally required.

The only times I really need to Remove a variable are where there's danger of using a stale value in a loop. It's something that's quite easy to do in VbScript when looping if insufficient error control or safeguards are in place.

I'd be interested to see what BSonPosh does with his, he's rather better at PowerShell than I :)

Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KTN-IT

ASKER

Thanks for your help!