Exercise 3: Why State Doesn't Persist Across -ComputerName Calls — Possible Solution ==================================================================== WHY THE VARIABLE DOESN'T PERSIST ------------------------------ Per this chapter's own warn-box, Invoke-Command -ComputerName opens a brand-new connection, runs the script block, and tears the entire connection back down again - every single time, even against the identical computer on a second call. There is no persistent session sitting in between the two calls for a variable to survive in; each -ComputerName call is a completely fresh, throwaway connection with its own fresh, empty scope, so a variable set during the first call is simply gone by the time the second call starts - it was never carried anywhere, because nothing existed to carry it. WHAT TO CHANGE TO MAKE IT PERSIST ------------------------------ Per this chapter, replacing -ComputerName with a persistent connection via New-PSSession, then using -Session (rather than -ComputerName) on each Invoke-Command call, is the fix: $session = New-PSSession -ComputerName Server01, followed by Invoke-Command -Session $session for each subsequent call. Because the same session object is reused across calls rather than a fresh connection being opened and closed each time, variables set during one call remain available in later calls against that same session. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly explains that -ComputerName creates a fresh, stateless connection on every call with no persistence mechanism, and correctly identifies New-PSSession plus -Session as the fix that makes state survive between calls.