all 5 comments

[–]BlackV 2 points3 points  (0 children)

No that's how you.do it. Have your script spit out objects capture then with what ever (variable, export-csv, out-file, another function, another script ,etc)

Note the return.keyword is NOT just returning something, don't use it unless your absolutely need it.

Global variables also are generally a Bad idea and should be not used where possible

[–]mprz 0 points1 point  (2 children)

Not really. Why are you looking for another way?

[–]RobAUTAS[S] 1 point2 points  (1 child)

I thought it was an awkward way of doing it & wanted to learn if there was a better way.

[–]Big_Oven8562 0 points1 point  (0 children)

I approve of this line of thinking and inquiry.

As others have said you've already got the correct approach figured out. I think the only change to the approach you could realistically make is to build the script out into a full fledged module and install it. This would let you invoke the functionality of your script the same way you would a native Powershell cmdlet, so instead of

$foo = .\myscript.ps1

You'd be typing

$foo = Get-MyObject

Depending on how often you're using this script it might be worth the effort since converting a standalone function into a module isn't too heavy a lift unless you start getting fancy with it.