使用 power shell 检索 txt 文件中的内容

tho*_*ier 2 powershell

我正在尝试编写一个 powershell 脚本,它将返回 txt 文件中的内容。但是,我不想指定 txt 文件的驱动器,因为 txt 文件将放置在与 powershell 脚本相同的文件夹中。

我正在使用这行代码:

获取内容 .\document.txt | 选择-第一个1-跳过1

但它不起作用。


里面的document.txt:

  • 这是第一行
  • 这是第二行

我应该编写什么脚本来检索第二行“这是第二行”,而不必输入像“C:\Data\Scripts\Document.txt”这样的完整路径?我搜索了在线解决方案,但许多解决方案要求我必须输入其目标路径。

Mar*_*agg 5

get-content $PSScriptRoot\document.txt | select -first 1 -skip 1
Run Code Online (Sandbox Code Playgroud)

请注意,$PSScriptRoot仅在脚本执行时才会有值,但它将表示脚本所在的路径。