当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

作者:精品下载站 日期:2024-12-14 04:55:55 浏览:16 分类:玩电脑

PowerShell 设置内容 | Set-Content 前 12 个参数及示例


[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

PowerShell 设置内容简介

在计算机中很多时候我们需要在文件中写入一些内容,这里的内容可以是字符串也可以是任何数值。为此,我们可以打开一个文件并写入我们所需的内容。但是 PowerShell 使我们能够在不打开文件的情况下将内容写入文件。这意味着我们可以通过命令行编写内容。所以 Set-Content 是帮助我们在任何文件系统上写入内容的命令。如果您了解 Linux,那么在 Linux 中它有一个名为 echo 的命令,可以在文件上写入内容PowerShell Set-Content 不仅限于在文件上写入内容,它还可以追加新内容、替换旧内容等。此命令的实时使用是,假设您有一个非常大的文件并且您想要替换整个文件中有一些单词,你可以直接写一行命令,单词就会被替换。

PowerShell Set-Content 的语法和参数

以下是 PowerShell Set-Content 的语法:

语法 #1: 不带 -Literalpath 的语法

Set-Content
[-Path] <array of strings are allowed in path>
[-Value] <Objects will be used here>
[-PassThru(return current content object which is holding it)]
[-Filter <filter string for which we are going to write for filter conditions>]
[-Include <any string that need to include in this operation>]
[-Exclude <any string that need to exclude in this operation>]
[-Force(file is busy or read only still it will allow to write contents)]
[-WhatIf(describe what will happen is command will execute)]
[-Confirm(asking before execution of command)]
[-NoNewline(without new line and space concatenation of string)]
[-Encoding <here we can define types of contents>]
[<CommonParameters>]

语法 #2:使用 -LiteralPath 的语法

Set-Content
[-Value] <Objects will be used here>
-LiteralPath <mention exact location>
[-PassThru(return current content object which is holding it)]
[-Include <any string that need to include in this operation>]
[-Exclude <any string that need to exclude in this operation>]
[-Force(file is busy or read only still it will allow to write contents)]
[-Credential ]
[-WhatIf(describe what will happen is command will execute)]
[-Confirm(asking before execution of command)]
[-NoNewline(without new line and space concatenation of string)]
[-Encoding <here we can define types of contents>]
[<CommonParameters>]

参数:

以下是 PowerShell Set-Content 的前 12 个参数:

  1. -确认:在执行 Set-Content 命令之前显示确认消息。始终建议使用这些命令,以避免任何重大错误。
  2. -编码:它允许我们提及目标文件的编码类型。它接受 ASCII、BigEndingUnicode、OEM、Unicode、UTF7、UTF8 等。
  3. -排除:将内容设置为目标文件内容时,可以排除特定字符串。我们可以使用通配符 (*)。
  4. -过滤器:您可以在内容写入之前定义过滤器。它还允许通配符(*)。
  5. -Force:当我们需要向任何目标文件写入内容时,即使该文件处于只读状态,也会使用强制命令。
  6. -包含:这里我们可以指定Set-Content操作中需要包含的字符串,在此操作中,我们还可以使用通配符(*)。
  7. -LiteralPath:如果您有精确路径,则会使用此命令,因为它不允许通配符或匹配属性。
  8. -NoNewline: 我们可以直接连接字符串,无需任何换行,也无需任何空格。基本上新行将添加在输出字符串的末尾。
  9. -PassThru: 显示保存内容的对象。默认情况下它不会返回任何内容。
  10. -路径:它定义了项目接收内容的路径。您可以在此处使用通配符。
  11. -WhatIf: 它显示命令的详细信息,或者只是程序执行后的结果。如果您想查看消息的输出或用途,则可以使用它。
  12. -值: 我们要在目标文件上写入的任何新内容。

实施 PowerShell 删除项目的示例

以下是实现 PowerShell Remove-Item 的示例:

例子#1

这是 Set-Content 的一个非常简单的示例,这里我们向现有文件添加一个字符串。我们可以在示例中看到,我们将 -Value 传递为“Hello, Friend my name is Ranjan”。它只是附加此字符串值。

实际执行的示例如下图所示。

Set-Content -Path ./test.txt -Value “Hello ,friends my name is Ranjan”
Get-Content -Path ./test.txt

输出:

[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

例子#2

在这里,我们将向不存在的文件写入内容,这意味着我们将创建一个新文件,然后附加字符串消息。请参阅下面给出的图像和示例。您可以对现有文件尝试相同的命令以查看输出的差异。

Set-Content -Path .\todatsDate.txt -Value (Get-Date)
Get-Content -Path ./todatsDate.txt

输出:

[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

示例#3

现在让我们举一个用另一个字符串替换字符串的例子。在下面的示例中,您可以看到,我们首先尝试将“Ranjan”替换为“Ajay”,但失败了,因为我们的字符串包含“Ranjan”而不是“Ranjan”,这意味着区分大小写。当我们再次尝试用“Ajay”替换“Ranjan”时,它的工作原理是“Ranjan”存在于我们现有的字符串中。请参阅下面给出的示例和屏幕。

(Get-Content ./test.txt).replace('Ranjan', 'ajay') | Set-Content ./test.txt

输出:

[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

示例#4

我们学习了如何在 PowerShell 中向文件附加字符串,现在让我们举一个示例,将多个字符串附加到多个文件。在下面的示例中,我们将“你好朋友,此内容将在所有三个文件上”附加到名称包含测试的所有文件。在该目录下,有三个文件test1.txt、test2.txt、test3.txt,并且所有文件都有相同的字符串。请参阅下面的示例屏幕。

Set-Content -Path .\test*.txt -Value 'Hello friend this content will be on all three file'
Get-Content -Path .\test*.txt

输出:

[玩转系统] PowerShell 设置内容 | Set-Content 前 12 个参数及示例

结论

我希望本教程足以简要介绍 Set-Content。在本教程中,我们了解如何在任何目标文件上添加替换字符串。只需编写一个命令即可在一次执行时在一个或多个文件上替换或写入内容,从而减少了我们执行太多手动工作以在文件系统上执行任何重复性工作的工作量。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯