Add-Content for each line in a CSV using PowerShell

Since you are using Import-Csv to read your file, which I assume is a proper CSV, you can use Export-Csv to output custom objects.

# Open file
$file = Import-Csv 'filepath'

# Loop through each line of CSV
# store results in $resultTable
$resultTable = foreach ($line in $file)
{       
    # Assign file path, and regex just the extension
    $path = $line.path
    $extension = $path -replace '.*\\Extensions\\'

    # Open chrome webstore for specific extension
    $result = Invoke-WebRequest -Uri "https://chrome.google.com/webstore/detail/$extension" -Method Get
    
    # Grab title of extension and place in CSV
    $title = $result.ParsedHtml.title
    
    # Create and output custom object with path,extension,title properties
    [pscustomobject]@{
        Path = $path
        Extension = $extension
        Title = $title
    }
}
# Export to CSV overwriting 'filepath'
$resultTable | Export-Csv 'filepath' -NoType

Export-Csv converts an input object into a CSV. Each property of the object becomes a column and each property value is output under those respective columns. Each object becomes its own row in the case of an array of objects.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top