简体   繁体   中英

In Powershell, save and restore array of PSCustomObjects to a file for later reload and re-use

Is there a way to write a the output of Get-ChildItem to a file and preserved the array of [PSCustomObjects], and then later, read this file and convert it back into an array of [PSCustomObjects]?

$gci = get-childitem -recurse

# How to implement this
save_file -file "list.gci". -value $gci

# How to implement this 
$gci2 = load_file -file "list.gci" 

foreach ($item in $gci2) {
    $fullname = $gci2.fullname
    $CreatationDate = $gci2.CreationDate
    write-host "$fullname"
}

Expanding on Olaf's comment, Powershell can save complex objects with their type info and properties intact to files using CLIXML :

# export to file
$gci = Get-ChildItem -Recurse
$gci | Export-Clixml c:\path\to\file.xml

# import later:
$gci2 = Import-Clixml c:\path\to\file.xml

# objects are still FileInfo objects:
$gci2 | Get-Member

   TypeName: Deserialized.System.IO.FileInfo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM