PowerShell Genie

PowerShell CSV

Export a predictable table and check imported types before using the values in calculations.

Lesson 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Overview

A useful CSV report has an intentional schema: choose the columns with Select-Object before exporting. Do not pipe Format-Table into Export-Csv; that exports formatting metadata instead of your service properties.

ConvertTo-Csv returns CSV lines without writing a file. Export-Csv writes a file and normally produces no success-stream output. The lesson uses conversion so you can inspect the header and rows first.

Use -NoTypeInformation for Windows PowerShell 5.1 compatibility. Encoding defaults differ by version; even -Encoding UTF8 differs in byte-order-mark behavior between 5.1 and modern PowerShell. Choose the encoding expected by the receiving system, especially for names containing non-ASCII characters.

Import-Csv creates objects whose field values are strings. A size column containing 100 and 20 can sort as text. Validate and convert that column before doing numeric work. Arrays and nested objects also need an explicit representation, such as joining a list into a field or choosing JSON instead.

See the objects move

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-Service | Where-Object Status -eq 'Running' | Select-Object -First 5 Name, Status, StartType | ConvertTo-Csv -NoTypeInformation
  1. Stage 1Get-ServicePositional values or no named parameters
  2. Stage 2Where-Object-eq
  3. Stage 3Select-Object-First
  4. Stage 4ConvertTo-Csv-NoTypeInformation

    An example to inspect

    Read the example, then complete the activity below. All lessons are public. Sign-in is optional for progress sync.

    Get-Service |
    Where-Object Status -eq 'Running' |
    Select-Object -First 5 Name, Status, StartType |
    ConvertTo-Csv -NoTypeInformation

    What happens

    The output starts with the quoted column names, followed by one line per selected service. Commas or quotes inside values are escaped by the CSV serializer; splitting each line on commas is not a reliable replacement for Import-Csv.

    After a file export, import it again and inspect the expected columns, row count and one representative value. File existence alone does not establish that the report is usable.

    Worth remembering

    Select a stable schema, preserve data objects until export, and validate imported field types.

    Primary documentation

    Microsoft Learn: export-csv

    Try it

    Browser dataset output · the real environment will differ

    1PS> Get-Service |
    2PS> Where-Object Status -eq 'Running' |
    3PS> Select-Object -First 5 Name, Status, StartType |
    4PS> ConvertTo-Csv -NoTypeInformation
    Browser simulation · fictional service data

    Converts selected service properties to CSV text without writing a file.

    Show reference output

    Browser dataset output · the real environment will differ

    "Name","Status","StartType"
    "BITS","Running","Manual"
    "Dnscache","Running","Automatic"
    "EventLog","Running","Automatic"
    "Spooler","Running","Automatic"
    "W32Time","Running","Manual"

    FIRST REPORT · EXPORT USABLE DATA

    Practise without copying

    Produce CSV for automatic-start services sorted by Name, with exactly Name and Status. Use ConvertTo-Csv -NoTypeInformation here; the browser does not write a file.

    How the worked example fits together

    A file that exists can still contain the wrong report. Inspect the header, row count and representative values. ConvertTo-Csv lets you check serialization here; Export-Csv performs the file write in real PowerShell. For cross-version exports, choose the encoding expected by the receiving application.

    Get-Service | Where-Object StartType -eq 'Automatic' | Sort-Object Name | Select-Object Name, Status | ConvertTo-Csv -NoTypeInformation

    The header and three data records match. In real PowerShell, replace ConvertTo-Csv with Export-Csv -Path ./services.csv -NoTypeInformation, then import the file and check its contents.

    Bounded browser simulation · fictional services · no computer or tenant access.

    Predict the result, write your pipeline, then run it.

    Practise this

    Write your first report

    Save Name and Status for all services to C:\Lab\report.csv without type information.

    Review the lesson

    Keep going

    Next: PowerShell JSON
    Genie guidePowerShell CSV

    Export a predictable table and check imported types before using the values in calculations.