peter0816
Goto Top

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

Content-Key: 312135

Url: https://administrator.de/contentid/312135

Ausgedruckt am: 28.03.2024 um 18:03 Uhr

Mitglied: 129813
129813 09.08.2016 aktualisiert um 11:56:11 Uhr
Goto Top
I don't see any problem here
(Get-WindowsFeature | ?{$_.Installed -eq $true}).Name | set-content C:\export.txt
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
"$($features -join "`r`n")" | set-content C:\export.txt  
Regards
Mitglied: Peter0816
Peter0816 09.08.2016 um 13:29:18 Uhr
Goto Top
Hallo,

das sieht soweit schon gut aus, nur möchte ich nun auch noch z.B. einen Platzhalter in einer Textdatei ersetzen:
$Features = (Get-WindowsFeature | ?{$_.Installed -eq $true}).Name
Get-Content .\export.txt | Foreach-Object {$_ -replace '#features#', $features} | Set-Content .\export.txt

In der Datei steht dann z.B. folgendes:

Installierte Features:
#Features#

Hier wird alles in eine Zeile geschrieben, wie bekomme ich die Umbrüche wieder her?

Mit freundlichen Grüßen

PEter
Mitglied: 129813
129813 09.08.2016 aktualisiert um 13:33:39 Uhr
Goto Top
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 face-smile
 ........... -replace '#features#',($features -join "`r`n")  ...............  
Mitglied: Peter0816
Peter0816 09.08.2016 um 14:00:37 Uhr
Goto Top
funktioniert bei mir leider nicht:

Foreach-Object {$_ -replace '#features#', ($features -join "`r`n") }
Mitglied: 129813
Lösung 129813 09.08.2016 aktualisiert um 14:10:43 Uhr
Goto Top
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   
Mitglied: Peter0816
Peter0816 09.08.2016 um 14:31:25 Uhr
Goto Top
Hallo,

jetzt verstehe ich was er damit meint, mit -join kann ich begrenzer zwischen den Arrays eingeben.
Vielen Dank!

Mfg

Peter