I have this content in a file:
File Versions
---- --------
aaa {1.0.0.123, 1.0.0.124}
bbb {1.0.0.123, 1.0.0.124}
How to read it in powershell into table, or typed array?
I've created the file by this script:
$versions = $dictionary | ForEach-Object{
$properties = @{
File = $dictionary.Key
Versions = $dictionary.Value <# an array of strings #>
}
New-Object PSObject -Property $properties
}
$versions | Out-File "filesVersions.txt"
The script is not used anymore. It's just for demostration of what is in the filesVersions.txt
file.
What I actually want is to store a key value pairs in a file, where key is FileName and Value is list of versions.
From time to time I need to read the file content, all a new row or a new version to existing row and save it back.
I wanted to use Format-Table
output for it.
Format-Table
output to a file. Do you have control over that output. It would be easier to address that problem then to parse it back now. Or is it actually a CSV file and you could just useImport-CSV "filesVersions.csv"
. Or is this a large file and that is why you useReadAllLines
? – Matt$versions
? – Matt{1.0.0.123, 1.0.0.124}
represents an array (of what? could be versions, IP addresses) or just the string literal"{1.0.0.123, 1.0.0.124}"
? As @Matt said, exporting to another format in the first place is how you'd solve this problem in the real world (eg.Export-CliXml
orExport-Csv
instead of formatted output toOut-File
). – Mathias R. Jessen