Returning multiple values from a PowerShell function

Returning multiple values in a powershell function is something that took me an embarrassing amount of time to figure out. Its not documented all that well and there are very few blog posts on the topic. It turns out there are a few ways to do this but I keep falling back to using hashtables, they are the most dynamic and pretty much anything can be returned with almost no issues. Below is a very simple example of how to make this work.

  1. Create a hashtable with: “[hastable]$varname = @{}”.
  2. Then do all of the work you need done and save your results in a property that you would like.
  3. Return the parent variable name and then you are able to access it via the properties you made.

Hope this helps!

 

1 comment… add one

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.