I am working on this bit of code that helps clean up a larger csv file it takes 7 seconds when measuring the Foreach statement.. how do I speed this up.
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') }
$null = $FileBrowser.ShowDialog()
$FilePath = $FileBrowser.FileName
$export = import-csv $FilePath
$exportFinalHeading = "Id,Type,Name,SpaceId,SpaceName,NetworkSwitchName,NetworkSwitchPortID,ExternalID,SerialNumber"
Add-Content -Path ".\ExportFinal.csv" -Value $exportFinalHeading
Measure-Command {ForEach ($export in $export){
$id = $($export.id) + ","
$Type = $($export.Type) + ","
$Name = $($export.Name) + ","
$SpaceId = $($export.SpaceId) + ","
$SpaceName = $($export.SpaceName) + ","
$NetworkSwitchName = $($export.NetworkSwitchName) + ","
$NetworkSwitchPortID = $($export.NetworkSwitchPortId) + ","
$ExternalID = $($export.ExternalId) + ","
$ExternalID = $ExternalID.replace(':',',')
$exportFinal = $id + $Type + $Name + $SpaceId + $SpaceName + $NetworkSwitchName + $NetworkSwitchPortID + $ExternalID
Add-Content -Path ".\ExportFinal.csv" -Value $exportFinal
}}
I have tried measuring just a straight pass thru of the text write and re write but the goal was to complete pull specific data out