Powershell array zeilenweise in Textdatei ausgeben
Hallo,
ich habe folgendes vor:
$features = Get-WindowsFeature | Where installed -eq true
$features = $features.name
und die Ausgabe am Bildschirm erscheint auch richtig:
$features
FileAndStorage-Services
Storage-Services
NET-Framework-45-Features
NET-Framework-45-Core
NET-WCF-Services45
NET-WCF-TCP-PortSharing45
RSAT
RSAT-Feature-Tools
RSAT-SNMP
FS-SMB1
usw...
nun möchte ich das ganze array in eine Textdatei schreiben,
jedoch steht es in der Textdatei dann so:
FileAndStorage-Services Storage-Services NET-Framework-45-Features NET-Framework-45-Core NET-WCF-Services45 NET-WCF-TCP-PortSharing45 RSAT RSAT-Feature-Tools RSAT-SNMP FS-SMB1
Wie kann ich es trennen mit "," oder ";" oder als Zeilenumbruch?
Greife mit get-content auf die Datei zu und mit Set-Content wird die Datei wieder geschlossen.
Danke
Peter
ich habe folgendes vor:
$features = Get-WindowsFeature | Where installed -eq true
$features = $features.name
und die Ausgabe am Bildschirm erscheint auch richtig:
$features
FileAndStorage-Services
Storage-Services
NET-Framework-45-Features
NET-Framework-45-Core
NET-WCF-Services45
NET-WCF-TCP-PortSharing45
RSAT
RSAT-Feature-Tools
RSAT-SNMP
FS-SMB1
usw...
nun möchte ich das ganze array in eine Textdatei schreiben,
jedoch steht es in der Textdatei dann so:
FileAndStorage-Services Storage-Services NET-Framework-45-Features NET-Framework-45-Core NET-WCF-Services45 NET-WCF-TCP-PortSharing45 RSAT RSAT-Feature-Tools RSAT-SNMP FS-SMB1
Wie kann ich es trennen mit "," oder ";" oder als Zeilenumbruch?
Greife mit get-content auf die Datei zu und mit Set-Content wird die Datei wieder geschlossen.
Danke
Peter
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 312135
Url: https://administrator.de/forum/powershell-array-zeilenweise-in-textdatei-ausgeben-312135.html
Ausgedruckt am: 30.04.2025 um 17:04 Uhr
6 Kommentare
Neuester Kommentar

I don't see any problem here
Don't write your array into quotationmarks ("") if you want do that you have to expand and join the array to a string like this
Regards
(Get-WindowsFeature | ?{$_.Installed -eq $true}).Name | set-content C:\export.txt
"$($features -join "`r`n")" | set-content C:\export.txt

Zitat von @Peter0816:
Hier wird alles in eine Zeile geschrieben, wie bekomme ich die Umbrüche wieder her?
Look in my second code above, there you find it Hier wird alles in eine Zeile geschrieben, wie bekomme ich die Umbrüche wieder her?
........... -replace '#features#',($features -join "`r`n") ...............

Sure this works 100%, but you have to import the text first, you are piping the text-inout in the pipeline and want to write to it the same time that doesn't work you have to add braces () around the text-import-statement of Get-Content, then you can write to the same file, otherwise you read and write to the same file at the same time, which will never work!
$features = (Get-WindowsFeature | ?{$_.Installed -eq $true}).Name
(Get-Content .\export.txt) -replace '#features#',($features -join "`r`n")| Set-Content .\export.txt