Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Export-CSV: Output Data to CSV File Using PowerShell

March 15, 2024

Export-CSV: Output Data to CSV File Using PowerShell

You can use the Export-CSV cmdlet in PowerShell to export data arrays to CSV files. In this article, we will show you how to export data to a CSV file and how to add additional rows or columns.

The CSV file is a plain text file format used to store tabular data, where values are separated by a special character (delimiter). The Export-CSV cmdlet converts the resulting PowerShell object to a comma-separated list of strings and saves the strings to the specified file.

The following command exports the list of Windows services to a CSV file:

Get-Service |select-object Name,DisplayName,Status | Export-CSV "C:\PS\services.CSV" -NoTypeInformation -Encoding UTF8

You can now open the CSV file you have received in any text editor. As you can see, the first row contains the column names (attributes of the PowerShell object) that we selected by using the Select-Object cmdlet, and then we can see the data line by line, separated by commas.

csv file format with delimeter

The comma is used as a separator (delimiter) in CSV by default. You can specify another CSV delimiter character (for example, semicolon, colon, etc.) with the –Delimiter option.

For example, let’s use a semicolon as a separator:

Get-Service | Export-CSV "C:\PS\services.CSV" -NoTypeInformation -Encoding UTF8 -Delimiter ";"

You can use the delimiter depending on your Windows regional settings. Use the -UseCulture parameter for this.

Get-Process | Export-Csv "c:\ps\process.csv" -UseCulture

You can also find out the default separator character from the regional settings of Windows:

(Get-Culture).TextInfo.ListSeparator

By default, the Export-CSV cmdlet creates a new CSV file (if the file already exists, it is overwritten/replaced by the new one). Use the -Append option, if you need to add new rows to the existing CSV file

For example, you want to schedule a PowerShell script that checks the free disk space and adds the current value to a CSV file:

$cur_time=get-date -Format u
$freedisksize=Get-CimInstance -Class Win32_LogicalDisk |Where-Object {$_.DeviceID -eq "C:"} |select DeviceID,FreeSpace

Now you need to add the current date to the table (object) as one of the fields (attributes):

$freedisksize| add-member -membertype NoteProperty -name Date -value $cur_time

Export your PowerShell object to a CSV file:

$freedisksize| Export-Csv -Path C:\ps\freespace.csv -Delimiter ";" -NoTypeInformation -Append

Powershell Append CSV file

You can also use the following additional Export CSV options:

  • -Encoding – allows to set CSV file encoding (utf8NOBOM is used by default). In most cases, I specify UTF8 here;
  • -Force – allows overwriting the read-only file;
  • -NoClobber – if the file already exists, do not overwrite it;
  • -Header – add a header to the CSV file (if it is missing);
  • -IncludeTypeInformation/-NoTypeInformation – add or skip the #TYPE line with object type data to the file header (for example, #TYPE System.Diagnostics.Process or #TYPE System.Service.ServiceController). In PowerShell 6+, the header TYPE information is not displayed by default;
  • -UseQuotes (introduces in PowerShell Core 7.x) – whether to quote values or not (AsNeeded/ Always (default)/Never)

You can then process the CSV file you receive in Excel or other programs.

You can write data directly to an Excel file from within PowerShell.

The Export-CSV cmdlet is often used to create various tabular exports and reports. Below are some examples of useful system administrator reports that can be generated using the Export-CSV command:

  • Exporting a list of computers in AD using the Get-ADComputer cmdlet;
  • Extract user information from Active Directory using the Get-ADUser;
  • Find out inactive users or computers in an AD domain
  • Getting user sign-in logs from the Azure AD;
  • Export RDP connections history (logs);
  • Script to check the Windows activation status on computers in a domain.
You can use the Import-CSV cmdlet to read (import) data from a CSV file into PowerShell.
0 comment
0
Facebook Twitter Google + Pinterest
PowerShellQuestions and AnswersWindows 10Windows Server 2019
previous post
How to Force Remove a Printer That Won’t Uninstall on Windows
next post
Security Tab Missing from File/Folder Properties in Windows

Related Reading

Fix: Remote Desktop Licensing Mode is not Configured

August 24, 2023

Wi-Fi (Internet) Disconnects After Sleep or Hibernation on...

March 15, 2024

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

How to Find the Source of Account Lockouts...

March 12, 2024

Managing Windows Firewall Rules with PowerShell

March 11, 2024

How to Delete Old User Profiles in Windows

March 15, 2024

Start Menu or Taskbar Search Not Working in...

April 22, 2025

How to Backup and Restore Websites and IIS...

June 8, 2023

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • Fix: Remote Desktop Licensing Mode is not Configured
  • How to Delete Old User Profiles in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top