How to store variables to disk so you can use them next time the PowerShell script runs? How to store variables to disk so you can use them next time the PowerShell script runs? powershell powershell

How to store variables to disk so you can use them next time the PowerShell script runs?


$foo | Export-CliXml foo.xml

then later

$foo = Import-CliXml foo.xml

Note that if $foo represents a live object, when you restore it, you are only going to get its properties. However the type information is more-or-less preserved. For example if you save out a System.Diagnostics.Process object, when you rehydrate it you will have a Deserialzed.System.Diagnostics.Process object.

BTW if you need to store/retrieve multiple variables, you can do that like so:

Get-Variable bla* | Export-Clixml vars.xmlImport-Clixml .\vars.xml | %{ Set-Variable $_.Name $_.Value }