Create CSV records using loop

Hello,
Newbie to Powershell

I need to build a script to create CSV by reading another CSV,

CSV 1-
Manufacturer Model Type Count
HP hp elitebook 840 Laptop 3
Dell dell probook 6560b Laptop 2

I need another CSV to be created like below based on number of count for that model,

Model Type
hp elitebook 840 Laptop
hp elitebook 840 Laptop
hp elitebook 840 Laptop
dell probook 6560b Laptop
dell probook 6560b Laptop

Please help with this

$CSV1 = @()
$CSV1 += [PSCustomObject]@{
    Manufacturer = 'HP'
    Model        = 'hp elitebook 840'
    Type         = 'Laptop'
    Count        = 3
}
$CSV1 += [PSCustomObject]@{
    Manufacturer = 'Dell'
    Model        = 'dell probook 6560b'
    Type         = 'Laptop'
    Count        = 2
}
$CSV1

$CSV2 = foreach ($Record in $CSV1) {
    1..$Record.Count | foreach  {
        [PSCustomObject]@{
            Model = $Record.Model 
            Type  = $Record.Type
        }
    }
}
$CSV2

[pre]

$csvfile = Get-Content -Path “$env:TEMP\ServerList.csv” | ConvertFrom-Csv

$csvlist = @()

foreach($csvrec in $csvfile) {
1…$csvrec.count | ForEach-Object {
$csvlist += $csvrec | Select Company,Model,Type
}
}

$csvlist | ConvertTo-Csv | Out-File -FilePath “$env:TEMP\ServerList2.csv”

[/pre]

Thanks Kiran. it worked for me…

one more thing, How do I add and update custom object like computer name , serial number to csv2 using same logic?

I am not really sure what exactly you are looking for, you can pipe custom object to ConvertTo-Csv CmdLet.

[pre]
$object | ConvertTo-Csv | Out-File -Path “$env:Temp\object.csv”
[/pre]