Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active January 9, 2026 23:33
Show Gist options
  • Select an option

  • Save santisq/a3a105840baa783fea82b19515f55565 to your computer and use it in GitHub Desktop.

Select an option

Save santisq/a3a105840baa783fea82b19515f55565 to your computer and use it in GitHub Desktop.
Adapted `Import-Csv` source to output `IEnumerable<T>`
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
benchmarks/
BenchmarkDotNet.Artifacts/
tools/dotnet
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
*.zip
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
project.fragment.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
#*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
### Custom entries ###
bin/
lib/
obj/
output/
tools/Modules
test.settings.json
tests/integration/.vagrant
tests/integration/cert_setup
tools/ProjectBuilder/output
tools/ProjectBuilder/bin
tools/ProjectBuilder/debug
tools/ProjectBuilder/obj
// Adapted from PowerShell's Import-Csv implementation to be used in standalone C# projects.
// https://github.com/PowerShell/PowerShell/blob/4838a8d5f6c95339d4b44c8b402165e69e6ff929/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
using System.Management.Automation;
using System.Reflection;
using System.Text;
namespace CsvUtils;
public static class Parser
{
private const int ValueCountGuestimate = 16;
private const int LineLengthGuestimate = 256;
private const string CRLF = "\r\n";
private const string LF = "\n";
private const string CR = "\r";
private static readonly HashSet<Type> s_numericTypes = [
typeof(int), typeof(long), typeof(double),
typeof(float), typeof(decimal), typeof(short),
typeof(ushort), typeof(byte), typeof(sbyte),
typeof(uint), typeof(ulong)
];
public static IEnumerable<T> Read<T>(string path, Encoding encoding)
=> Read<T>(path, encoding: encoding);
public static IEnumerable<T> Read<T>(
string path,
char delimiter = ',',
Encoding? encoding = null)
where T : class, new()
{
using StreamReader sr = new(path, encoding ?? Encoding.UTF8);
string[] header = ReadHeader(sr, delimiter);
List<string> values = new(ValueCountGuestimate);
StringBuilder builder = new(LineLengthGuestimate);
Action<T, string>[] setters = [.. GetSetters<T>(header)];
while (true)
{
ParseNextRecord(sr, values, builder, delimiter);
if (values.Count == 0)
{
break;
}
if (values.Count == 1 && string.IsNullOrEmpty(values[0]))
{
continue;
}
T outobj = new();
for (int i = 0; i < setters.Length; i++)
{
setters[i](outobj, values[i]);
}
yield return outobj;
}
}
private static IEnumerable<Action<T, string>> GetSetters<T>(string[] properties)
{
Type type = typeof(T);
foreach (string property in properties)
{
PropertyInfo info = type.GetProperty(
property,
BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public)
?? throw new ArgumentNullException($"Property '{property}' not found on type '{type}'.");
if (!info.CanWrite)
throw new ArgumentException($"Property '{property}' on '{type}' is read-only.");
MethodInfo setter = info.GetSetMethod()
?? throw new ArgumentException($"Setter for '{property}' is inaccessible.");
ParameterExpression target = Expression.Parameter(type, "target");
ParameterExpression value = Expression.Parameter(typeof(string), "value");
MethodCallExpression body = Expression.Call(
target,
setter,
GetArgumentExpression(info.PropertyType, value));
yield return new(
Expression
.Lambda<Action<T, string>>(body, target, value)
.Compile());
}
}
private static Expression GetArgumentExpression(
Type type,
ParameterExpression value)
{
static MethodCallExpression ParseNumeric(
Type numericType,
ParameterExpression value)
{
MethodInfo parseMethod = numericType.GetMethod(
"Parse",
[typeof(string), typeof(NumberStyles), typeof(IFormatProvider)])
?? throw new InvalidOperationException($"No suitable Parse overload found on {numericType}");
return Expression.Call(
parseMethod,
value,
Expression.Constant(NumberStyles.Any),
Expression.Constant(CultureInfo.InvariantCulture));
}
return type switch
{
_ when type == typeof(string) || type == typeof(object) => value,
_ when s_numericTypes.Contains(type) => ParseNumeric(type, value),
_ when type == typeof(DateTime) =>
Expression.Call(
typeof(DateTime),
nameof(DateTime.Parse),
null,
value,
Expression.Constant(CultureInfo.InvariantCulture),
Expression.Constant(DateTimeStyles.None)),
_ when type == typeof(bool) =>
Expression.Call(typeof(bool), nameof(bool.Parse), null, value),
_ when type == typeof(Guid) =>
Expression.Call(typeof(Guid), nameof(Guid.Parse), null, value),
_ => Expression.Call(
typeof(LanguagePrimitives),
nameof(LanguagePrimitives.ConvertTo),
[type], value)
};
}
private static bool NextCharIs(this StreamReader sr, char c)
{
int i = sr.Peek();
return i != -1 && c == (char)i;
}
private static string[] ReadHeader(StreamReader sr, char delimiter)
{
List<string> values = new(ValueCountGuestimate);
StringBuilder builder = new(LineLengthGuestimate);
while (!sr.EndOfStream)
{
ParseNextRecord(sr, values, builder, delimiter);
while (values.Count > 1 && values[^1] == string.Empty)
{
values.RemoveAt(values.Count - 1);
}
if (values.Count != 0 && values[0].StartsWith("#Fields: "))
{
values[0] = values[0][9..];
break;
}
if (values.Count != 0 && values[0].StartsWith('#'))
{
continue;
}
break;
}
ValidatePropertyNames(values);
return [.. values];
}
private static void ValidatePropertyNames(List<string> names)
{
if (names.Count == 0)
{
return;
}
HashSet<string> headers = new(StringComparer.OrdinalIgnoreCase);
foreach (string currentHeader in names)
{
if (!string.IsNullOrEmpty(currentHeader) && !headers.Add(currentHeader))
{
throw new ArgumentOutOfRangeException($"'{currentHeader}' is duplicated in header.");
}
}
}
private static void ParseNextRecord(
StreamReader sr,
List<string> result,
StringBuilder current,
char delimiter)
{
result.Clear();
current.Clear();
bool seenBeginQuote = false;
while (!sr.EndOfStream)
{
char ch = (char)sr.Read();
if (ch == delimiter)
{
if (seenBeginQuote)
{
current.Append(ch);
continue;
}
result.Add(current.ToString());
current.Clear();
continue;
}
if (ch == '"')
{
if (seenBeginQuote)
{
if (sr.NextCharIs('"'))
{
sr.Read();
current.Append('"');
continue;
}
seenBeginQuote = false;
bool endofRecord = false;
ReadTillNextDelimiter(sr, current, ref endofRecord, true, delimiter);
result.Add(current.ToString());
current.Clear();
if (endofRecord) break;
continue;
}
if (current.Length == 0)
{
seenBeginQuote = true;
continue;
}
bool endOfRecord = false;
current.Append(ch);
ReadTillNextDelimiter(sr, current, ref endOfRecord, false, delimiter);
result.Add(current.ToString());
current.Clear();
if (endOfRecord) break;
continue;
}
if (ch == ' ' || ch == '\t')
{
if (seenBeginQuote)
{
current.Append(ch);
continue;
}
if (current.Length == 0)
continue;
bool endOfRecord = false;
current.Append(ch);
ReadTillNextDelimiter(sr, current, ref endOfRecord, true, delimiter);
result.Add(current.ToString());
current.Clear();
if (endOfRecord) break;
continue;
}
if (IsNewLine(sr, ch, out string newLine))
{
if (seenBeginQuote)
{
current.Append(newLine);
continue;
}
result.Add(current.ToString());
current.Clear();
break;
}
current.Append(ch);
}
if (current.Length != 0)
result.Add(current.ToString());
}
private static void ReadTillNextDelimiter(
StreamReader sr,
StringBuilder current,
ref bool endOfRecord,
bool eatTrailingBlanks,
char delimiter)
{
int distance = 0;
while (!sr.EndOfStream)
{
char ch = (char)sr.Read();
if (ch == delimiter)
break;
if (IsNewLine(sr, ch, out string _))
{
endOfRecord = true;
break;
}
current.Append(ch);
if (ch == ' ' || ch == '\t')
{
distance++;
continue;
}
distance = 0;
}
if (eatTrailingBlanks)
{
current.Length -= distance;
}
}
private static bool IsNewLine(
StreamReader sr,
char ch,
out string newLine)
{
newLine = string.Empty;
if (ch == '\n')
{
newLine = LF;
return true;
}
if (ch != '\r')
{
return false;
}
if (!sr.NextCharIs('\n'))
{
newLine = CR;
return true;
}
sr.Read();
newLine = CRLF;
return true;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>CsvUtils</AssemblyName>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management.Automation" Version="7.4.0" PrivateAssets="all" />
</ItemGroup>
</Project>

Usage

PS> Get-Content .\test.csv | Select-Object -First 5

# "Status","Name","DisplayName"
# "Running","NlaSvc","Network Location Awareness"
# "Stopped","ConsentUxUserSvc_396777c8","ConsentUX_396777c8"
# "Stopped","SensorService","Sensor Service"
# "Stopped","svsvc","Spot Verifier"

Define a class that matches the schema of your CSV then call the Read<> method using your type as the generic argument.
For Add-Type, you can either inline the code or put in a file (like in the example here).

class Service {
    [System.ServiceProcess.ServiceControllerStatus] $Status
    [string] $Name
    [string] $DisplayName
}

Add-Type (Get-Content .\ImportCsv.cs -Raw) -WA 0 -IgnoreWarnings
[CsvUtils.Parser]::Read[Service]('.\test.csv')

#  Status Name                      DisplayName
#  ------ ----                      -----------
# Running NlaSvc                    Network Location Awareness
# Stopped ConsentUxUserSvc_396777c8 ConsentUX_396777c8
# Stopped SensorService             Sensor Service
# Stopped svsvc                     Spot Verifier
# Stopped dmwappushservice          Device Management Wireless Application Protocol (WAP) ...
# Stopped DmEnrollmentSvc           Device Management Enrollment Service
# Stopped NcdAutoSetup              Network Connected Devices Auto-Setup
# Stopped NcaSvc                    Network Connectivity Assistant
# Stopped dot3svc                   Wired AutoConfig
# Stopped TroubleshootingSvc        Recommended Troubleshooting Service

Performance on a Csv with 1 million rows

Test Average (5 invocations) RelativeSpeed AverageMemory (Mb)
Utils.CsvParser 00:00:01.626 1x 368.06
Import-Csv 00:00:05.376 3.31x 999.74

Performance Test

$ErrorActionPreference = 'Stop'

$base = 'https://gist.githubusercontent.com/santisq/'
$measure = 'bd3d1d47c89f030be1b4e57b92baaddd/raw/Measure-Expression.ps1'
$importcsv = 'a3a105840baa783fea82b19515f55565/raw/CsvUtils.cs'

Add-Type (Invoke-RestMethod "${base}${importcsv}") -WA 0 -IgnoreWarnings
Invoke-RestMethod "${base}${measure}" | Invoke-Expression

try {
    $serv = Get-Service |
        Select-Object Status, Name, DisplayName -First 10 |
        ConvertTo-Csv -NoHeader |
        Join-String -Separator "`n"

    $lines = 100000 # x 10 for `$serv` 10 lines
    $tmp = New-TemporaryFile
    $writer = [System.IO.StreamWriter]::new($tmp.FullName)
    $writer.WriteLine('"Status","Name","DisplayName"')

    while ($lines--) {
        $writer.WriteLine($serv)
    }

    $writer.Dispose()

    class Service {
        [string] $Status
        [string] $Name
        [string] $DisplayName
    }

    Measure-Expression -TestCount 5 @{
        'Utils.CsvParser' = {
            param($path)

            foreach ($i in [CsvUtils.Parser]::Read[Service]($path)) {
                $i
            }
        }
        'Import-Csv'      = {
            param($path)

            Import-Csv $path
        }
    } -Parameters @{ path = $tmp.FullName }
}
finally {
    ${tmp}?.Delete()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment