Add-Content für FileSystem

Letzte Aktualisierung: Oktober 2014

Betrifft: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

Fügt Inhalt, z. B. Wörter oder Daten, an eine Datei an.

Syntax

Add-Content [-Encoding {<Unknown> | <String> | <Unicode> | <Byte> | <BigEndianUnicode> | <UTF8> | <UTF7> | <UTF32> | <Ascii> | <Default> | <Oem>}] [-Force] [-Stream <string>] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]

Beschreibung

In Dateisystemlaufwerken fügt das Add-Content-Cmdlet den von Ihnen angegebenen Inhalt an das Ende einer Datei an. Dieses Cmdlet gilt nicht für Ordner.

Hinweis: In dieser Hilfedatei für benutzerdefinierter Cmdlets wird erläutert, wie das Add-Content-Cmdlet in einem Dateisystemlaufwerk funktioniert. Um Informationen zum Add-Content-Cmdlet auf allen Laufwerken zu erhalten, geben Sie "Get-HelpAdd-Content -Pfad $null" ein oder siehe Add-Content unter https://go.microsoft.com/fwlink/?LinkID=113278.

Parameter

-Encoding <FileSystemCmdletProviderEncoding>

Gibt die Dateicodierung an. Die Standardeinstellung ist ASCII.

Gültige Werte:

-- ASCII: Verwendet die Codierung für den ASCII-Zeichensatz (7-Bit).

-- BigEndianUnicode: Codiert in UTF-16-Format mit Big-Endian-Bytereihenfolge.

-- Byte: Codiert eine Reihe von Zeichen in eine Folge von Bytes.

– String: Verwendet den Codierungstyp für eine Zeichenfolge.

-- Unicode: Codiert in UTF-16-Format mit Little-Endian-Bytereihenfolge.

-- UTF7: Codiert in UTF-7-Format.

-- UTF8: Codiert in UTF-8-Format.

-- Unknown: Der Typ der Codierung ist unbekannt oder ungültig. Die Daten können als Binärdateien behandelt werden.

Encoding ist ein dynamischer Parameter, den der FileSystem-Anbieter zum Add-Content-Cmdlet hinzufügt. Dieser Parameter funktioniert nur in Dateisystemlaufwerken.

Erforderlich?

falsch

Position?

benannt

Standardwert

ASCII

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Force

Fügt Inhalt an Dateien an, auch wenn sie schreibgeschützt sind. Ohne diesen Parameter sind schreibgeschützte Dateien nicht betroffen.

Erforderlich?

falsch

Position?

benannt

Standardwert

False

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Stream <string>

Fügt den Inhalt zum angegebenen alternativen Datenstrom hinzu. Wenn der Strom noch nicht vorhanden ist, erstellt ihn Add-Content. Geben Sie den Namen des Stroms ein. Platzhalter werden nicht unterstützt.

Stream ist ein dynamischer Parameter, den der FileSystem-Anbieter zum Add-Content-Cmdlet hinzufügt. Dieser Parameter funktioniert nur in Dateisystemlaufwerken.

Dieser Parameter wird in Windows PowerShell 3.0 eingeführt.

Erforderlich?

falsch

Position?

benannt

Standardwert

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Confirm

Fordert Sie zur Bestätigung auf, bevor der Befehl ausgeführt wird.

Erforderlich?

falsch

Position?

benannt

Standardwert

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-WhatIf

Beschreibung der Folgen einer Ausführung des Befehls, ohne dass der Befehl dabei tatsächlich ausgeführt wird.

Erforderlich?

falsch

Position?

benannt

Standardwert

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-UseTransaction

Schließt den Befehl in die aktive Transaktion ein. Dieser Parameter ist nur gültig, wenn eine Transaktion ausgeführt wird. Weitere Informationen finden Sie unter about_Transactions

Erforderlich?

falsch

Position?

benannt

Standardwert

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

<CommonParameters>

Dieses Cmdlet unterstützt folgende allgemeine Parameter: -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, -OutVariable, -Verbose, -WarningAction und -WarningVariable. Weitere Informationen finden Sie unter about_CommonParameters.

Ein- und Ausgaben

Der Eingabetyp ist der Typ der Objekte, die an das Cmdlet übergeben werden können. Der Rückgabetyp ist der Typ der Objekte, die das Cmdlet zurückgibt.

Eingaben

Sytem.Object[], System.String[], System.Management.Automation.PSCredential

Sie können den Wert (Objekt), einen Pfad oder ein Objekt mit Anmeldeinformationen an Add-Content übergeben.

Ausgaben

None oder System.String

Wenn Sie den Passthru-Parameter verwenden, generiert %%amp;quot;Add-Content%%amp;quot; ein System.String-Objekt, das den Inhalt darstellt. Andernfalls wird von diesem Cmdlet keine Ausgabe generiert.

Beispiel 1

C:\PS>Add-Content -Path *.txt -Exclude help* -Value "END"

Description
-----------
This command adds "END" to all text files in the current directory, except for those with file names that begin with "help."





Beispiel 2

C:\PS>Add-Content -Path file1.log, file2.log -Value (get-date) -PassThru

Description
-----------
This command adds the date to the end of the File1.log and File2.log files and then displays the date at the command line. 

The command uses the Get-Date cmdlet to get the date, and it uses the Value parameter to pass the date to Add-Content. The PassThru parameter sends the added content through the pipeline. Because there is no other cmdlet to receive the passed content, it is displayed at the command line.





Beispiel 3

C:\PS>Add-Content -Path monthly.txt -Value (Get-Content c:\rec1\weekly.txt)

Description
-----------
This command adds the contents of the Weekly.txt file to the end of the Monthly.txt file. It uses the Get-Content cmdlet to get the contents of the Weekly.txt file, and it uses the Value parameter to pass the content of weekly.txt to Add-Content. The parentheses ensure that the Get-Content command is complete before the Add-Content command begins.

You can also copy the content of Weekly.txt to a variable, such as $w, and then use the Value parameter to pass the variable to Add-Content. In that case, the command would be "add-content -path monthly.txt -value $w".





Beispiel 4

C:\PS>Add-Content -Value (Get-Content test.log) -Path C:\tests\test134\logs\test134.log

Description
-----------
This command creates a new directory and file and copies the content of an existing file to the newly created file.

This command uses the Add-Content cmdlet to add the content. The value of the Value parameter is a Get-Content command that gets content from an existing file, Test.log. 

The value of the path parameter is a path that does not exist when the command runs. In this example, only the C:\Tests directories exist. The command creates the remaining directories and the Test134.log file.

The Force parameter is not required for this command. Add-Content creates directories to complete a path even without the Force parameter.





Beispiel 5

C:\PS>Get-Content test.xml | Add-Content final.xml -Force -Encoding UTF8

Description
-----------
This command appends the contents of the final.xml file to the contents of the test.xml file. 

The command uses the Force parameter so that the command is successful even if the Final.xml file is read-only. It uses the Encoding parameter to specify an encoding of UTF-8.





See Also

Concepts

FileSystem Provider

Other Resources

Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path