Get the last object returned


Occasionally I execute a PowerShell command and I forget to store its return values/objects in a variable. Does PowerShell store the returned object of the last command in a variable I could access to?


From stuffing the output of the last command into an automatic variable: override out-default and store the results in a global variable called $lastobject.

For powershell 6 and newer:

For powershell 5:

And for both:

Solution posted by Andy Schneider and inspired by comments from “//\o//” and Joel.


Get the last object returned by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply