Cleaning up After Yourself in PowerShell

PowerShell makes so many things so much easier. If like me, you come from the world of C++, then you've known the horrors of memory management and the dreaded memory leak. Like many higher-level programming languages, PowerShell handles a lot of the memory management for you, and you rarely get to see memory leaks.

With such handholding from PowerShell, it's easy to become complacent and forget about best practices in programming. An essential rule that comes straight from your mother's mouth: clean up after yourself.


That's why Koupi steps have a Clean Up setting. It's important when creating code to not leave a mess. It's very easy to forget about a file here and there, but that adds up every time you run a new piece of code.


I am a messy person, and I often forget to do that, even in real life. Therefore, I figured I would write a post about proper artifact management in PowerShell so we all can work on writing cleaner code, quite literally.

Koupi allows you to create PowerShell scripts without needing to write a single line of code. Just add the steps you want to perform, fill in the blanks, download the script and run it. It's that easy. Give it a try here (It's free).

Where do we need to clean?

There are two places that garbage accumulates on a computer: Memory and storage. When talking about PowerShell, another place garbage accumulates is the PowerShell session itself.


Memory

Memory is the fastest interface on a computer. Data stored in memory can be retrieved almost instantly, and, and for most applications, it's considered instant for both read and write operations. That's why the vast majority of operations in programming are storing data in memory.

When scripting, people don't think about memory management until it becomes problematics. That's why it's essential to keep an eye on the variables created and the data associated with them as you write your code.

Professionally, I have written code that runs for days and even months. That one CSV I loaded once may seem harmless, but if the data is only used at the beginning of the code, it will be in memory for as long as it is running. A few kilobytes or even megabytes may not have much of an impact, but if every piece of code running on a computer was created with this reasoning, we'd be running out of memory very quickly.


In PowerShell, there are a few ways to handle those variables:

  1. Assigning them the value of a $null pointer

  2. Remove-Variable to remove the variable and its value.

Disk

The disk is one place that people don't always think about cleaning up. After all, storage is cheap and almost unlimited; what's a little temp file? Well, if you ever had to clean up space on your computer, you'll know that temp files can add up to a LOT of data.

Granted, it can be a hassle to clean up after yourself, especially if it doesn't seem like it will have much impact, but the computer user will be grateful.


To easily clean up your file system mess, you can keep track of your files and delete them in that way:


Session

The PowerShell session can often be neglected when it comes to clean up. Sure, the resources will be deallocated when the session ends, but what if you want to clean up in the middle of a session?


You can use the following code to clean up the current session. Just put it at the beginning of your code and call the appropriate functions when needed.


I hope this will help you with keeping your environment clean and free of clutter.


Let me know in the comments what you want to read about next.

Koupi allows you to create PowerShell scripts without needing to write a single line of code. Just add the steps you want to perform, fill in the blanks, download the script and run it. It's that easy. Give it a try here (It's free).


©2019-2020 by Bulles Technologies. Proudly created with Wix.com