PowerShell text file and csv read / write


PowerShell text file and csv read / write

This article is a summary of examples for creating and reading text and CSV files in PowerShell.

Create text file

'Name','Nummer' | out-file c:\temp\text.txt

Create CSV file

'Name,Nummer' | out-file c:\temp\text.csv

Attach CSV file content

'Walter,007' | out-file c:\temp\text.csv -Append

Read text file

Get-Content c:\temp\text.txt

CSV file from an object

By means of Export-CSV objects can be exported to a CSV file:

get-counter | Export-CSV test.csv -append

"append" adds values to an existing .csv file and creates a .csv file if none exists

Possibly useful additional parameters:

-Encoding UTF8 ... encodes the file in UTF8, may be needed in case of problems with umlauts

-Delimiter ";" ... Instead of "," as separator ";" is used

-NoTypeInformation -Force ... disables the first line in the CSV where PowerShell stores information about the file.

Read CSV file

Import-csv c:\temp\text.csv

Import-csv c:\temp\text.csv | select -ExpandProperty Nummer

read out certain entries from the CSV file

Import-csv c:\temp\text.csv | Where-Object {$_.Name -eq 'Walter'}

see also:  PowerShell Syntax: compare and nest 

read specific values from the CSV file

$(Import-csv c:\temp\text.csv | Where-Object {$_.Name -eq 'Walter'}).Nummer

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-05-02 from Bernhard | Übersetzung Deutsch |🔔 | Comments:0

PowerShell - Best Practice - creating better scripts | PowerShell | Using PowerShell to set file system permissions: ACL

Top articles in this section

PowerShell: Prevent screen saver, lock: Move mouse regularly

Alternatively, if you can't change the screen lock settings, you can move the mouse regularly, or have a script move the mouse. Originally published as an AutoIt script, I recreated the script with a few PowerShell lines. Anyone who copies the following commands into a PowerShell session will prevent the computer from locking the screen or starting the screensaver:

PowerShell Log-Files: Logging into a textfile - write to file

Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.

PowerShell TCP Listener

PowerShell uses the following commands to open a socket on a specific port via System.NET:

Questions / Comments