Set-Content
Записать текст в файл
Параметры
-Path путь к файлу с возможностью указания подстановочных знаков
-LiteralPath дословный путь к файлу
-Encoding кодировка файла
ascii: Uses the encoding for the ASCII (7-bit) character set
bigendianunicode: Encodes in UTF-16 format using the big-endian byte order
bigendianutf32: Encodes in UTF-32 format using the big-endian byte order
oem: Uses the default encoding for MS-DOS and console programs
unicode: Encodes in UTF-16 format using the little-endian byte order
utf7: Encodes in UTF-7 format
utf8: Encodes in UTF-8 format
utf8BOM: Encodes in UTF-8 format with Byte Order Mark (BOM)
utf8NoBOM: Encodes in UTF-8 format without Byte Order Mark (BOM)
utf32: Encodes in UTF-32 format
Примеры
Записать текст в файл
C:\file.txt.Wake up, Neo, select all
Set-Content C:\file.txt "My Text"
полное описание
➡️➡️ ПЕРЕВОД ⬅️⬅️

Set-Content
SYNOPSIS
Writes new content or replaces existing content in a file.
SYNTAX
Path (Default)
Код: Выделить всё
Set-Content [-Path] <string[]> [-Value] <Object[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-NoNewline] [-Encoding <FileSystemCmdletProviderEncoding>] [-Stream <string>] [<CommonParameters>]LiteralPath
Код: Выделить всё
Set-Content [-Value] <Object[]> -LiteralPath <string[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-NoNewline] [-Encoding <FileSystemCmdletProviderEncoding>] [-Stream <string>] [<CommonParameters>]DESCRIPTION
Set-Contentis a string-processing cmdlet that writes new content or replaces the content in a
file.Set-Contentreplaces the existing content and differs from theAdd-Contentcmdlet that
appends content to a file. To send content toSet-Contentyou can use the Value parameter on
the command line or send content through the pipeline.If you need to create files or directories for the following examples, see New-Item.
EXAMPLES
Example 1: Replace the contents of multiple files in a directory
This example replaces the content for multiple files in the current directory.
Код: Выделить всё
Get-ChildItem -Path .\Test*.txtКод: Выделить всё
Test1.txt Test2.txt Test3.txtКод: Выделить всё
Set-Content -Path .\Test*.txt -Value 'Hello, World' Get-Content -Path .\Test*.txtКод: Выделить всё
Hello, World Hello, World Hello, WorldThe
Get-ChildItemcmdlet uses the Path parameter to list .txt files that begin with
Test*in the current directory. TheSet-Contentcmdlet uses the Path parameter to specify
theTest*.txtfiles. The Value parameter provides the text string Hello, World that
replaces the existing content in each file. TheGet-Contentcmdlet uses the Path parameter to
specify theTest*.txtfiles and displays each file's content in the PowerShell console.Example 2: Create a new file and write content
This example creates a new file and writes the current date and time to the file.
Код: Выделить всё
Set-Content -Path .\DateTime.txt -Value (Get-Date) Get-Content -Path .\DateTime.txtКод: Выделить всё
1/30/2019 09:55:08Set-Contentuses the Path and Value parameters to create a new file named DateTime.txt
in the current directory. The Value parameter usesGet-Dateto get the current date and time.
Set-Contentwrites the DateTime object to the file as a string. TheGet-Contentcmdlet uses
the Path parameter to display the content of DateTime.txt in the PowerShell console.Example 3: Replace text in a file
This command replaces all instances of word within an existing file.
Код: Выделить всё
Get-Content -Path .\Notice.txtКод: Выделить всё
Warning Replace Warning with a new word. The word Warning was replaced.Код: Выделить всё
(Get-Content -Path .\Notice.txt) | ForEach-Object {$_ -Replace 'Warning', 'Caution'} | Set-Content -Path .\Notice.txt Get-Content -Path .\Notice.txtКод: Выделить всё
Caution Replace Caution with a new word. The word Caution was replaced.The
Get-Contentcmdlet uses the Path parameter to specify the Notice.txt file in the
current directory. TheGet-Contentcommand is wrapped with parentheses so that the command
finishes before being sent down the pipeline.The contents of the Notice.txt file are sent down the pipeline to the
ForEach-Objectcmdlet.
ForEach-Objectuses the automatic variable$_and replaces each occurrence of Warning with
Caution. The objects are sent down the pipeline to theSet-Contentcmdlet.Set-Contentuses
the Path parameter to specify the Notice.txt file and writes the updated content to the
file.The last
Get-Contentcmdlet displays the updated file content in the PowerShell console.Example 4: Use Filters with Set-Content
You can specify a filter to the
Set-Contentcmdlet. When using filters to qualify the Path
parameter, you need to include a trailing asterisk (*) to indicate the contents of the
path.The following command set the content all
*.txtfiles in theC:\Temp
directory to the Value empty.Код: Выделить всё
Set-Content -Path C:\Temp\* -Filter *.txt -Value "Empty"PARAMETERS
-Credential
[!NOTE]
This parameter is not supported by any providers installed with PowerShell.
To impersonate another user, or elevate your credentials when running this cmdlet,
use Invoke-Command.Код: Выделить всё
Type: System.Management.Automation.PSCredential Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: True (ByPropertyName) Accept wildcard characters: False-Encoding
Specifies the type of encoding for the target file. The default value is
Default.Encoding is a dynamic parameter that the FileSystem provider adds to
Set-Content. This parameter
works only in file system drives.The acceptable values for this parameter are as follows:
AsciiUses ASCII (7-bit) character set.BigEndianUnicodeUses UTF-16 with the big-endian byte order.BigEndianUTF32Uses UTF-32 with the big-endian byte order.ByteEncodes a set of characters into a sequence of bytes.DefaultUses the encoding that corresponds to the system's active code page (usually ANSI).OemUses the encoding that corresponds to the system's current OEM code page.StringSame asUnicode.UnicodeUses UTF-16 with the little-endian byte order.UnknownSame asUnicode.UTF7Uses UTF-7.UTF8Uses UTF-8.UTF32Uses UTF-32 with the little-endian byte order.
Encoding is a dynamic parameter that the FileSystem provider adds to
Set-Content. This parameter
works only in file system drives.Код: Выделить всё
Type: Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding Parameter Sets: (All) Aliases: Accepted values: ASCII, BigEndianUnicode, BigEndianUTF32, Byte, Default, OEM, String, Unicode, Unknown, UTF7, UTF8, UTF32 Required: False Position: Named Default value: Default Accept pipeline input: False Accept wildcard characters: False-Exclude
Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value
of this parameter qualifies the Path parameter. Enter a path element or pattern, such as
*.txt. Wildcard characters are permitted. The Exclude parameter is effective only when the
command includes the contents of an item, such asC:\Windows\*, where the wildcard character
specifies the contents of theC:\Windowsdirectory.Код: Выделить всё
Type: System.String[] Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: True-Filter
Specifies a filter to qualify the Path parameter. The FileSystem provider is the only installed PowerShell provider that supports the use of filters.
Filters are more efficient than other parameters, because the provider applies them when the cmdlet
gets the objects rather than having PowerShell filter the objects after they are retrieved.Код: Выделить всё
Type: System.String Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: True-Force
Forces the cmdlet to set the contents of a file, even if the file is read-only. Implementation
varies from provider to provider.
The Force parameter does not override security restrictions.Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False-Include
Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value
of this parameter qualifies the Path parameter. Enter a path element or pattern, such as
"*.txt". Wildcard characters are permitted. The Include parameter is effective only when the
command includes the contents of an item, such asC:\Windows\*, where the wildcard character
specifies the contents of theC:\Windowsdirectory.Код: Выделить всё
Type: System.String[] Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: True-LiteralPath
Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is
typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose
it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters
as escape sequences.Код: Выделить всё
Type: System.String[] Parameter Sets: LiteralPath Aliases: PSPath Required: True Position: Named Default value: None Accept pipeline input: True (ByPropertyName) Accept wildcard characters: False-NoNewline
The string representations of the input objects are concatenated to form the output. No spaces or
newlines are inserted between the output strings. No newline is added after the last output string.Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False-PassThru
Returns an object that represents the content. By default, this cmdlet does not generate any output.
Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False-Path
Specifies the path of the item that receives the content.
Wildcard characters are permitted.Код: Выделить всё
Type: System.String[] Parameter Sets: Path Aliases: Required: True Position: 0 Default value: None Accept pipeline input: True (ByPropertyName) Accept wildcard characters: True-Stream
Specifies an alternative data stream for content. If the stream does not exist, this cmdlet creates
it. Wildcard characters are not supported.Stream is a dynamic parameter that the FileSystem provider adds to
Set-Content. This
parameter works only in file system drives.You can use the
Set-Contentcmdlet to change the content of the Zone.Identifier alternate data
stream. However, we do not recommend this as a way to eliminate security checks that block files
that are downloaded from the Internet. If you verify that a downloaded file is safe, use the
Unblock-Filecmdlet.This parameter was introduced in PowerShell 3.0.
Код: Выделить всё
Type: System.String Parameter Sets: (All) Aliases: Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False-UseTransaction
Includes the command in the active transaction. This parameter is valid only when a transaction is
in progress.Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: usetx Required: False Position: Named Default value: False Accept pipeline input: False Accept wildcard characters: False-Value
Specifies the new content for the item.
Код: Выделить всё
Type: System.Object[] Parameter Sets: (All) Aliases: Required: True Position: 1 Default value: None Accept pipeline input: True (ByPropertyName, ByValue) Accept wildcard characters: False-Confirm
Prompts you for confirmation before running the cmdlet.
Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: cf Required: False Position: Named Default value: False Accept pipeline input: False Accept wildcard characters: False-WhatIf
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Код: Выделить всё
Type: System.Management.Automation.SwitchParameter Parameter Sets: (All) Aliases: wi Required: False Position: Named Default value: False Accept pipeline input: False Accept wildcard characters: FalseCommonParameters
This cmdlet supports the common parameters:
-Debug,-ErrorAction,-ErrorVariable,
-InformationAction,-InformationVariable,-OutVariable,-OutBuffer,-PipelineVariable,
-Verbose,-WarningAction, and-WarningVariable.INPUTS
System.Object
You can pipe an object that contains the new value for the item to
Set-Content.OUTPUTS
None or System.String
When you use the PassThru parameter,
Set-Contentgenerates a System.String object that
represents the content. Otherwise, this cmdlet does not generate any output.NOTES
- You can also refer to
Set-Contentby its built-in alias,sc. Set-Contentis designed for string processing. If you pipe non-string objects toSet-Content,
it converts the object to a string before writing it. To write objects to files, useOut-File.- The
Set-Contentcmdlet is designed to work with the data exposed by any provider. To list the
providers available in your session, typeGet-PsProvider.