views:

79

answers:

1

Hi,

I would like to store a variable to disk or registry so I can use it the next time I run the scheduled script? Preferably an one-liner or two...

Cheers, Roy

+5  A: 
$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.xml
Import-Clixml .\vars.xml | %{ Set-Variable $_.Name $_.Value }
Keith Hill
Perfect! Works like a charm ;) Only needed to store one integer... Tnx!
SilverViper
If you don't need full type information, other options are Get-Content/Set-Content and Import-CSV/Export-CSV.
JasonMArcher