PowerShell Function Output, Memory Management And ConvertTo-HTML

In this post I’m going to cover PowerShell function output, memory management and ConvertTo-HTML.

Function Output And Return Keyword
In PowerShell the Return keyword is just an exit point for your function, it doesn’t explicitly set the return value of your function. Everything written to the output stream will be returned by your function, calling return and optionally with a value to return will just add to the output of your function.

Sometimes you end up with extra output returned from you function which you may not want. To determine where the extra output came from, do not capture the result as a variable, rather let it go to the console so you can see at which point it is written. Pipe the offending cmdlets to Out-Null to suppress the output.

Memory Management
Since PowerShell is using .NET behind the scenes the same garbage collection rules apply. Memory management is not usually a problem if you execute the script and close PowerShell afterwards since the memory will be reclaimed. If you need to clean up memory during the execution of the script use Remove-Variable or Clear-Variable, depending on whether you want to use the variable again later in the script. This will make the memory from that variable available for garbage collection. The .NET CLR will do a garbage collection if there is memory pressure you don’t have to call Gc.Collect() explicitly unless your script can’t handle the delay when garbage collection happens. In which case you should call GC.collect() at a point in time where it is acceptable for your script but this usually applies to real time systems not the tasks you script with PowerShell.

ConvertTo-HTML And Strings
If you try to create an HTML table from the contents of a string array you will see something like this:

PowerShell Function Output, Memory Management And ConvertTo-HTML

This is because ConvertTo-HTML builds up the table using the properties of the input object, provided they can be displayed. A string object only has two properties Length and Chars[Int32] so it is adding the Length property to the table. To fix this you can make your own object and add a property to it that will be used in the table, in this case Text.

$strings = @('one', 'two', 'three')
 $newstrings = ($strings | ForEach-Object  { new-object -TypeName PSObject -Property @{"Text" = $_.ToString() } } )
 $html = $newstrings  |  ConvertTo-Html

PowerShell Function Output, Memory Management And ConvertTo-HTML

You can also instruct ConvertTo-HTML to include or exclude properties if you don’t want all the properties to show in the table.

Note: You can use Add-Member to add properties to PSObjects only so we couldn’t add a member to .NET string objects but if you for instance used Get-Content to read the strings from a file they will be PSObjects and you can add your own properties to the existing objects without creating new ones.

$PSObjectStrings | ForEach-Object { Add-Member -InputObject $_ -Type NoteProperty -Name "Text" -Value $_.ToString() }

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *