Get-Content für FileSystem

Letzte Aktualisierung: Mai 2014

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

Ruft den Inhalt einer Datei ab.

Syntax

Get-Content [-Delimiter <string>] [-Encoding {<Unknown> | <String> | <Unicode> | <Byte> | <BigEndianUnicode> | <UTF8> | <UTF7> | <UTF32> | <Ascii> | <Default> | <Oem>}] [-Force] [-Raw <switch>] [-Stream <string>] [-Wait] [-UseTransaction] [<CommonParameters>]

Beschreibung

In Dateisystemlaufwerken können Sie mit dem Cmdlet Get-Content Inhalt abrufen, den Sie in der Befehlszeile anzeigen, in einer Variablen für die Verarbeitung speichern oder in eine andere Datei schreiben. Es gilt nicht für Ordner.

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

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 Get-Content-Cmdlet hinzufügt. Dieser Parameter funktioniert nur in Dateisystemlaufwerken.

Verwenden Sie beim Lesen und Schreiben in Binärdateien einen Wert Byte für die Codierung dynamischer Parameter und der Wert 0 für den Parameter ReadCount. Ist der ReadCount-Wert 0, wird die gesamte Datei in einem einzigen Lesevorgang gelesen und in ein einzelnes Objekt (PSObject) konvertiert. Der standardmäßige ReadCount-Wert 1 liest ein Byte in jedem Lesevorgang und konvertiert jedes Byte in ein separates Objekt, was Fehler verursacht, wenn Sie mithilfe des Set-Content-Cmdlets die Bytes in eine Datei schreiben. Weitere Informationen finden Sie in den Beispielen.

Erforderlich?

falsch

Position?

benannt

Standardwert

ASCII

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Delimiter <string>

Gibt das Trennzeichen an, das Get-Content verwendet, um die Datei während des Lesevorgangs in Objekte zu unterteilen.

Der Standardwert ist "\n", das End-of-Line-Zeichen.

Aus diesem Grund gibt Get-Content beim Lesen einer Textdatei standardmäßig eine Sammlung von Zeichenfolgeobjekten zurück, von denen jedes mit einem End-of-Line-Zeichen endet.

Wenn Sie ein Trennzeichen eingeben, das in der Datei nicht vorhanden ist, gibt Get-Content die gesamte Datei als ein einzelnes unbegrenztes Objekt zurück.

Sie können diesen Parameter verwenden, um eine große Datei in kleinere Dateien aufzuteilen, indem Sie ein Dateitrennzeichen wie z. B. "Ende des Beispiels" als Trennzeichen angeben. Das Trennzeichen wird beibehalten (nicht verworfen) und wird das letzte Element in jedem Dateiabschnitt.

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

Hinweis zur Fehlerbehebung: Wenn der Wert des Delimiter-Parameters eine leere Zeichenfolge ist, gibt Get-Content zurzeit nichts zurück. Dies ist ein bekanntes Problem. Geben Sie einen Wert ein, der in der Datei nicht vorhanden ist, um Get-Content zu zwingen, die gesamte Datei als einzelne, unbegrenzte Zeichenfolge zurückzugeben.

Erforderlich?

falsch

Position?

benannt

Standardwert

End-of-Line-Zeichen

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Force

Ruft den Inhalt aller Dateien ab, einschließlich ausgeblendeter Dateien. Standardmäßig ruft Get-Content den Inhalt von ausgeblendeten Dateien nicht ab, es sei denn, Sie geben die ausgeblendete Datei mit Namen an.

Erforderlich?

falsch

Position?

benannt

Standardwert

False

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Raw <switch>

Ignoriert Zeilenumbruchzeichen und gibt den gesamten Inhalt einer Datei in einer Zeichenfolge zurück. Standardmäßig wird der Inhalt einer Datei als ein Array von Zeichenfolgen, die durch Zeilenumbruchzeichen getrennt sind, zurückgegeben.

Raw ist ein dynamischer Parameter, den der FileSystem-Anbieter zum Get-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

-Wait

Wartet darauf, dass das Cmdlet den Inhalt vor der Rückgabe der Eingabeaufforderung abruft. In der Warteschlange überprüft Get-Content die Datei einmal pro Sekunde, bis Sie dies unterbrechen, indem Sie z. B. STRG+C drücken.

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

Erforderlich?

falsch

Position?

benannt

Standardwert

False

Pipelineeingaben akzeptieren?

falsch

Platzhalterzeichen akzeptieren?

falsch

-Stream <string>

Ruft die Inhalte des angegebenen alternativen NTFS-Dateidatenstroms aus der Datei ab. Geben Sie den Namen des Stroms ein. Platzhalter werden nicht unterstützt.

Stream ist ein dynamischer Parameter, den der FileSystem-Anbieter zum Get-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

-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

System.Int64, System.String[], System.Management.Automation.PSCredential

Sie können die Anzahl der gelesenen, die Gesamtanzahl, Pfade oder Anmeldeinformationen an Get-Content übergeben.

Ausgaben

System.Object, System.String

Get-Content gibt Objekte zurück, die den Inhalt darstellen, den es abruft. Der Objekttyp hängt von den Inhaltstyp ab. Wenn Sie den Stream-Parameter verwenden, gibt das Cmdlet die alternativen Datenstrominhalte als Zeichenfolge zurück.

Beispiel 1

C:\PS>Get-Content -Path C:\Chapters\chapter1.txt

Description
-----------
This command gets the content of the Chapter1.txt file and displays it in the console. It uses the Path parameter to specify the name of the item. 

Get-Content actually passes the content down the pipeline, but because there are no other cmdlets in the pipeline, Windows PowerShell formats the contents and displays it in the console.





Beispiel 2

C:\PS>Get-Content C:\Logs\Log060912.txt -TotalCount 50 | Set-Content Sample.txt

Description
-----------
This command gets the first 50 lines of the Log060912.txt file and stores them in the sample.txt file. 

The command uses the Get-Content cmdlet to get the text in the file. (The name of Path parameter, which is optional, is omitted.) The TotalCount parameter limits the retrieval to the first 50 lines. The pipeline operator (|) sends the result to Set-Content, which places it in the sample.txt file.





Beispiel 3

C:\PS>(Get-Content Cmdlets.txt -TotalCount 5)[-1]

Description
-----------
This command gets the fifth line of the Cmdlets.txt text file. It uses the TotalCount parameter to get the first five lines and then uses array notation to get the last line (indicated by "-1") of the resulting set.





Beispiel 4

C:\PS>Get-Content .\DataSets\*.csv -Delimiter "*---*" -Force -Encoding UTF8

Description
-----------
This command gets the contents of all CSV files in the DataSets subdirectory. It uses the Force parameter to get all files, including hidden files, and the Encoding parameter to specify the file encoding. 

The command also uses the Delimiter parameter to divide the returned content into sets,  each of which ends at the CSV file row that contains the "*----*" marker.





Beispiel 5

C:\PS>Get-Content .\Copy-Scripts.ps1 -Stream Zone.Identifier

[ZoneTransfer]
ZoneId=3

Description
-----------
This command uses the Stream parameter to get the content of the Zone.Identifier alternate data stream. The output includes Zone ID value of 3, which represents the Internet.

The Stream parameter is introduced in Windows PowerShell 3.0.





Beispiel 6

C:\PS>$Manifest = (Get-Module -List PSScheduledJob).Path

C:\PS>$Hash = Invoke-Expression (Get-Content $Manifest -Raw)

C:\PS>$Hash

Name                           Value
----                           -----
Copyright                      © Microsoft Corporation. All rights reserved.
ModuleToProcess                Microsoft.PowerShell.ScheduledJob.dll
FormatsToProcess               PSScheduledJob.Format.ps1xml
PowerShellVersion              3.0
CompanyName                    Microsoft Corporation
GUID                           50cdb55f-5ab7-489f-9e94-4ec21ff51e59
Author                         Microsoft Corporation
CLRVersion                     4.0
CmdletsToExport                {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger...}
TypesToProcess                 PSScheduledJob.types.ps1xml
HelpInfoURI                    https://go.microsoft.com/fwlink/?LinkID=223911
ModuleVersion                  1.0.0.0

C:\PS>$Hash.ModuleToProcess
Microsoft.PowerShell.ScheduledJob.dll

Description
-----------
The commands in this example get the contents of a module manifest file (.psd1) as a hash table. The manifest file contains a hash table, but if you get the contents without the Raw dynamic parameter, it is returned as an array of newline-delimited strings.

The Raw dynamic parameter is introduced in Windows PowerShell 3.0.

The first command uses the Path property of modules to get the path to the file that contains the module manifest for the PSScheduledJob module. It saves the path in the $Manifest variable.

The second command uses the Invoke-Expression cmdlet to run a Get-Content command and the Raw dynamic parameter of the Get-Content cmdlet to get the contents of the module manifest file in a single string. The command saves the hash table in the $Hash variable.

The third command gets the hash table in the Hash variable. The contents is returned as a collection of name-value pairs.

The fourth command uses the ModuleToProcess property of the hash table to get the value of the ModuleToProcess key in the module manifest.





Beispiel 7

C:\PS>$a = Get-Content -Path .\Download.zip -Encoding Byte -ReadCount 0

Set-Content -Path \\Server\Share\Download.zip -Encoding Byte -Value $a


$b = Get-Content -Path .\Download.zip -Encoding Byte
Set-Content -Path \\Server\Share\Download.zip -Encoding Byte -Value $b

Set-Content : Cannot proceed with byte encoding. When using byte encoding the content must be of type byte.
At line:1 char:1
+ Set-Content \\Server\Share\Download.zip -Encoding Byte -Value $b 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetContentCommand

Description
-----------
This example shows how to use the ReadCount parameter of the Get-Content cmdlet with a value of 0 to avoid byte-related errors when using the Set-Content cmdlet to write the bytes to a file.

When getting the content of a file in bytes, Get-Content creates an object (PSObject) for the bytes in each read operation. If you read the bytes one at a time, which is the default, Get-Content creates an object for each byte. The objects cause errors when you use the Set-Content cmdlet to write the bytes to a file.

The first command uses the Get-Content cmdlet to get the contents of the Download.zip file and save it in the $a variable. The command uses the Encoding dynamic parameter with a value of Byte. It also uses the ReadCount parameter with a value of 0, which directs Get-Content to get the file contents in a single read operation. The default value of the ReadCount parameter, 1, gets one byte at a time.

The second command uses the Set-Content cmdlet to write the bytes in the $a variable to the Download.zip file on a file share. The command succeeds.

The third and fourth commands show the same sequence without the ReadCount parameter. 

The third command uses the Encoding dynamic parameter of the Get-Content cmdlet to get the contents of the Download.zip file and save it in the $b variable. Because the command omits the ReadCount parameter, it uses the default value of 1.

The fourth command uses the Set-Content cmdlet to write the bytes in the $b variable to the Download.zip file on a file share. Because the content is a collection of objects, rather than a single object that contains a byte array, the command fails.





See Also

Concepts

FileSystem Provider

Other Resources

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