I am looking to make my script a lot faster and giving me an output faster. I am dealing with large sets of data in my csv, and it takes around an hour if I put in the full file to complete with the script.
$csv = Import-Csv 'U:\Local Group Members.csv' |
Where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded"
#Modify each line
Foreach ($row in $csv) {
If ($row.Type0 -eq 'Domain') {
$row."Unique Account Name" = "$($row.Domain0) - $($row.Account0)"
If ($row."Unique Account Name" -in @('ACCOUNTS - DODSCAN'.'ACCOUNTS - Domain Admins','ACCOUNTS - LADM_WS_Admins','ACCOUNTS - Tech Enterprise'))
{$row."Excluded" = "True"}
Else {$row."Excluded" = "False"}
}
Else {
$row."Unique Account Name" = "$($row.Netbios_name0) - $($row.Account0)"
If ($row."Account0" -in @('esrxadm1n_esi','#Update','medco_tech','medco_admin'))
{$row."Excluded" = "True"}
Else {$row."Excluded" = "False"}
}
Write-Host $row."Unique Account Name"
Write-Host $row."Excluded"
}
#Export CSV
$csv | Export-Csv U:\$(get-date -f yyyy-MM-dd-hh-mm)-TestOutput1.csv -NoTypeInformation
Can anyone help me make it faster?
Write-Hostcommands could write to a file instead. In any event, posting the question on Code Review rather than here does sound like a good suggestion. I don't know enoughPowerShellto answer your question.O(n)in the number of rows. Printing to a console is slow. If the bottleneck isn't all of thoseWrite-Hostcommands then I have no idea what it is. Hopefully someone at Code Review (where working code is improved) would know.