I'm trying to avoid the extremely verbose hash maps and arrays, as commonly used in powershell. Why? Because I have 100's of lines, and it just doesn't make any sense to have to wrap every single line in a @(name='foo; id='bar')
etc.), when all I need is a CSV type of array.
$header = @('name', 'id', 'type', 'loc')
$mycsv = @(
# name, id, type, loc
'Brave', 'Brave.Brave', 1, 'winget'
'Adobe Acrobat (64-bit)', '{AC76BA86-1033-1033-7760-BC15014EA700}', 2, ''
'GitHub CLI', 'GitHub.cli', 3, 'C:\portable'
)
# Do some magic here to set the CSV / hash headers so I can use them as shown below
Foreach ($app in $mycsv) {
Write-Host "App Name: $app.name"
Write-Host "App Type: $app.type"
Write-Host "App id : $app.id"
Write-Host "App Loc : $app.type"
Write-Host ("-"*40)
}
I'm sure you see where I am going.
So how can I process the inline CSV line-by-line using the header names?
Expected output:
App Name: Brave
App Type: 1
App id : Brave.Brave
App Loc : winget
----------------------------------------
...
CodePudding user response:
You can use an in-memory string representation of CSV data and parse it into objects with ConvertFrom-Csv
:
$mycsv =
@'
name,id,type,loc
Brave,Brave.Brave,1,winget
Adobe Acrobat (64-bit),{AC76BA86-1033-1033-7760-BC15014EA700},2,
GitHub CLI,GitHub.cli,3,C:\portable
'@ | ConvertFrom-Csv
$mycsv | Format-List
then provides the desired output (without Format-List
, you'd get implicit Format-Table
formatting, because the objects have no more than 4 properties).
This is convenient, and even allows you to use incidental whitespace for better formatting, but invariably creates objects whose properties are all strings ([string
]-typed).
Use "..."
around field values that themselves contain ,
CodePudding user response:
To process inline CSV in a PowerShell script, you can use the ConvertFrom-Csv cmdlet to convert the CSV data into objects with properties that you can use in your script. Here is an example of how you could use this cmdlet to process the CSV data in your script:
$header = @('name', 'id', 'type', 'loc')
$mycsv = @(
# name, id, type, loc
'Brave', 'Brave.Brave', 1, 'winget'
'Adobe Acrobat (64-bit)', '{AC76BA86-1033-1033-7760-BC15014EA700}', 2, ''
'GitHub CLI', 'GitHub.cli', 3, 'C:\portable'
)
# Convert the CSV data into objects with properties
$apps = $mycsv | ConvertFrom-Csv -Header $header
Foreach ($app in $apps) {
Write-Host "App Name: $($app.name)"
Write-Host "App Type: $($app.type)"
Write-Host "App id : $($app.id)"
Write-Host "App Loc : $($app.loc)"
Write-Host ("-"*40)
}
This script uses the ConvertFrom-Csv cmdlet to convert the inline CSV data into objects with properties that match the values in the $header variable. It then uses a foreach loop to iterate over the objects in the $apps variable and prints the values of the properties for each object.
Note: In this example, the ConvertFrom-Csv cmdlet assumes that the first row of the CSV data contains the headers, which is why we need to specify the -Header parameter when calling the cmdlet. If your CSV data does not have headers, you can specify the property names using the -Property parameter instead. For example:
$mycsv = @(
# name, id, type, loc
'Brave', 'Brave.Brave', 1, 'winget'
'Adobe Acrobat (64-bit)', '{AC76BA86-1033-1033-7760-BC15014EA700}', 2, ''
'GitHub CLI', 'GitHub.cli', 3, 'C:\portable'
)
# Convert the CSV data into objects with properties
$apps = $mycsv | ConvertFrom-Csv -Property @('name', 'id', 'type', 'loc')
This script uses the ConvertFrom-Csv cmdlet to convert the inline CSV data into objects with properties that match the values specified in the -Property parameter. It then uses a foreach loop to iterate over the objects in the $apps variable and prints the values of the properties for each object.