PowerShell: Spalten tauschen und neue Spalten in CSV datei erzeugen
Hallo ,
ich nutze folgendes PowerShell Skript:
Ich habe folgendes Problem. Ich bearbeite und fasse zwei Typen von CSV Dateien zusammen, die sich in der Anordnung und Anzahl der Spalten unterscheiden.
CSV1: Header: "MesswertA","Land","MesswertB","MesswertC","MesswertD","Capacity","ReasonA","TimeStamp","MesswertE","PublicationTimeStamp","ModificationTimeStamp"
CSV2: Header: "MesswertA","Land","MesswertB","MesswertC","MesswertD","Capacity","TimeStamp","MesswertE","PublicationTimeStamp","ModificationTimeStamp"
Der Unterschied ist, dass in CSV1 in der 7. Spalte ein extra Wert steht, der in CSV2 nicht steht. Wenn ich nun alle Dateien über -recurse einlese und auch das Datum umformatieren lasse, dann funktioniert es so natürlich nicht. In CSV2 ist der "TimeStamp" in Spalte 7 und nicht wie bei CSV 1: in Spalte 8. Dementsprechend kann keine Umformatierung vorgenommen werden.
Meine Frage ist: Wie kann ich in dieses Skript einbauen, dass er CSV2: mit einer Spalte "ReasonA" ausstattet, die genau wie bei CSV 1 Spalte 7 darstellt. Zudem sollte diese Spalte mit Nullen gefüllt werden (eine Null pro Zeile). Die CSV Dateien sind eindeutig erkennbar an dem Namen, das heißt CSV1: heißt ....DEAT und CSV2: .... (also ohne DEAT).
Das Skript soll quasi alle Dateien in dem Ordner einlesen, erkennen um welchen CSV Typ es sich handelt, die Datumsformatierung für CSV Typ 1 wie gewohnt vornehmen und bei CSV2 erst die Spalte mit Nullen erzeugen und dann die Datumsformatierung vornehmen.
Das Ergebnis wäre eine CSV Datei: Die das richtige Datumsformat enthält und eine einheitliche Headerstruktur aufweist.
VG
Rippchen
ich nutze folgendes PowerShell Skript:
$folder = 'C:\Users\Laptop\Desktop\Import'
$out = 'C:\Users\Laptop\Desktop\Export\EXPORT.csv'
gci $folder -Filter *.csv -recurse | %{
$raw = ((gc $_.FullName) | select -Skip 7)
$csv = $raw[0..($raw.GetUpperBound(0)-1)]| ConvertFrom-CSV -Delimiter ";" -Header "MesswertA","Land","MesswertB","MesswertC","MesswertD","Capacity","ReasonA","TimeStamp","MesswertE","PublicationTimeStamp","ModificationTimeStamp" # CSV durch Semikolon getrennt, Überschriften der erzeugten Dateien werden festgelegt, die letzte Zeile jeder eingelesenen Datei wird gelöscht, da Prüfsumme.
$csv | %{$_."ModificationTimeStamp" = get-date $_."ModificationTimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."PublicationTimeStamp" = get-date $_."PublicationTimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."TimeStamp" = get-date $_."TimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | export-csv $out -Append -Delimiter ";" -Notype -Encoding UTF8
}
Ich habe folgendes Problem. Ich bearbeite und fasse zwei Typen von CSV Dateien zusammen, die sich in der Anordnung und Anzahl der Spalten unterscheiden.
CSV1: Header: "MesswertA","Land","MesswertB","MesswertC","MesswertD","Capacity","ReasonA","TimeStamp","MesswertE","PublicationTimeStamp","ModificationTimeStamp"
CSV2: Header: "MesswertA","Land","MesswertB","MesswertC","MesswertD","Capacity","TimeStamp","MesswertE","PublicationTimeStamp","ModificationTimeStamp"
Der Unterschied ist, dass in CSV1 in der 7. Spalte ein extra Wert steht, der in CSV2 nicht steht. Wenn ich nun alle Dateien über -recurse einlese und auch das Datum umformatieren lasse, dann funktioniert es so natürlich nicht. In CSV2 ist der "TimeStamp" in Spalte 7 und nicht wie bei CSV 1: in Spalte 8. Dementsprechend kann keine Umformatierung vorgenommen werden.
Meine Frage ist: Wie kann ich in dieses Skript einbauen, dass er CSV2: mit einer Spalte "ReasonA" ausstattet, die genau wie bei CSV 1 Spalte 7 darstellt. Zudem sollte diese Spalte mit Nullen gefüllt werden (eine Null pro Zeile). Die CSV Dateien sind eindeutig erkennbar an dem Namen, das heißt CSV1: heißt ....DEAT und CSV2: .... (also ohne DEAT).
Das Skript soll quasi alle Dateien in dem Ordner einlesen, erkennen um welchen CSV Typ es sich handelt, die Datumsformatierung für CSV Typ 1 wie gewohnt vornehmen und bei CSV2 erst die Spalte mit Nullen erzeugen und dann die Datumsformatierung vornehmen.
Das Ergebnis wäre eine CSV Datei: Die das richtige Datumsformat enthält und eine einheitliche Headerstruktur aufweist.
VG
Rippchen
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 309325
Url: https://administrator.de/forum/powershell-spalten-tauschen-und-neue-spalten-in-csv-datei-erzeugen-309325.html
Ausgedruckt am: 28.04.2025 um 18:04 Uhr
16 Kommentare
Neuester Kommentar

Hi.
With
you can add additional columns to a csv. To fill it with a "0" simply supply the -value property of add-member. Thats all you need.
Regards
With
$csv | add-member -MemberType NoteProperty -Name 'NameOfColumn' -Value 'ValueOfColumn' -Force
Regards

Ordering columns (in reality these are no columns but "properties") is the easiest thing .... You do it with a simple select-object statement with the names of all your columns in the desired order, before you export the object !
and so on. Really easy 
$csv | select Column1,Column2,Column3,........ | export-csv ..............

# check if property exists ...
if ($csv.ReasonA -ne $null){
# Type A
}else{
# Type B
}

If you don't check if there is a valid date value which can be interpreted as a date, this error is normal because a "null" value can't be converted into a date ! So check if the date columns are filled with valid data. Not every date time string value can be automatically converted to a date time value. Some formats need the format string to be passed as a parameter.

Zitat von @Rippchen:
I think the problem is that the script "thinks" that there is a "ReasonA" column. Because of that, the script thinks that the property "Timestamp" stands there were the property "Mix" stands.
No this is never possible, because you access your columns(in reality = properties) by property name!!!I think the problem is that the script "thinks" that there is a "ReasonA" column. Because of that, the script thinks that the property "Timestamp" stands there were the property "Mix" stands.
The property "ReasonA" stands bevore the property "Timestamp" so in this case the Timestamp stands were the property "Mix" in a type B file stands.
See comment above, this is not possible because you access the properties by name. The position is totally irrelevant !! The property names are defined by the heading column of the CSV file.We don't know how your files look like exactly
So if you want further help from me, contact me via personal message than i can make you an offer for a script.
Good luck.

Your script will never work because the variable $csv does not exist at the if check
, that's your error!!!!!!
You first have to check if the file has enough columns and then decide which way to go.
You first have to check if the file has enough columns and then decide which way to go.

$folder.ReasonA
???? Why did you try this ?? $folder is a plain string and never has a property ReasonA OMG $folder = 'C:\Import'
$out = 'C:\Export\Export.csv'
gci $folder -Filter *.csv -recurse | %{
$raw = gc $_.Fullname | select -skip 7
if ($raw.split(';').count -eq 11){
$csv = $raw[0..($raw.GetUpperBound(0)-1)]| ConvertFrom-CSV -Delimiter ";" -Header "CAR","Country","FUEl","NUMStartDate","NUMEndDate","Cost","ReasonA","TimeStamp","MIX","PublicationTimeStamp","ModificationTimeStamp"
}else{
$csv = $raw[0..($raw.GetUpperBound(0)-1)]| ConvertFrom-CSV -Delimiter ";" -Header "CAR","Country","FUEl","NUMStartDate","NUMEndDate","Cost","TimeStamp","MIX","PublicationTimeStamp","ModificationTimeStamp"
$csv | add-member -MemberType NoteProperty -Name "ReasonA" -Value '' -Force
}
$csv | add-member -MemberType NoteProperty -Name "NonavailabilityReason" -Value '' -Force
$csv | %{$_."NUMStartDate" = get-date $_."NUMStartDate" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."NUMEndDate" = get-date $_."NUMEndDate" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."TimeStamp" = get-date $_."TimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."PublicationTimeStamp" = get-date $_."PublicationTimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | %{$_."ModificationTimeStamp" = get-date $_."ModificationTimeStamp" -Format 'dd-MM-yyyy HH:mm:ss'}
$csv | select "CAR","Country","FUEl","NUMStartDate","NUMEndDate","Cost","ReasonA","TimeStamp","MIX","PublicationTimeStamp","ModificationTimeStamp"| export-csv $out -Append -Delimiter ";" -Notype -Encoding UTF8
}

It splits the first row (after skipping 7) into an array by using the semicolon as delimiter, then it counts the number of elements in the array with the property count to get the number of columns.