Extracting Values from a PowerShell import-csv cmdlet

Posted on 2009-04-24
Last Modified: 2012-06-27
Dear All,
I have a CSV file which I am reading using the import-csv cmdlet. The file is similar to:
The problem is are the fields at the end with the headers SCSI1, SCS2 etc. There could be anything from 1 to 32 of these. I am doing the standard powershell stuff to read the file and process each line in it record at a time as shown below. I can get to each field no bother but the group at the end are proving to be a problem. I need to turn the values in the SCSI fields into an array. If there were a fixed number I could just do @($R.SCSI1, $R.SCSI2....) but there could be quite a few and the number could vary line by line.

Does anyone have any thoughts on how this could be done?

Best wishes....
Colin Bruce
$recs = import-csv "data.csv"

foreach ($r in $recs)


  .......process fields here


Open in new window

Question by:CovUni
    LVL 70

    Accepted Solution


    Hi Colin,

    How about something like this?

    $Recs = @()
    Import-CSV "data.csv" | %{
      $Temp = @()
      ForEach ($Property in ($_ | Get-Member -Type NoteProperty -Name "SCSI*")) {
        If ($_.$($Property.Name) -ne $Null) { $Temp += $_.$($Property.Name) }
      $Recs += $_ | Select-Object NCPUs, Memory, @{n='SCSI';e={ $Temp }}

    Open in new window


    Author Closing Comment

    Dear Chris,
    Thanks for the reply and help. It worked perfectly. I am very impressed. I spent hours trying to do this and got nowhere. I owe you a beer at the very least.

    Best wishes...
    LVL 70

    Expert Comment

    by:Chris Dent

    Glad I could help out :)


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now