powershell - 使用powershell检索txt文件中的内容

标签 powershell

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

我正在使用这行代码:

get-content .\document.txt | select -first 1 -skip 1

但它不起作用。


document.txt 内部:

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

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

最佳答案

get-content $PSScriptRoot\document.txt | select -first 1 -skip 1

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

关于powershell - 使用powershell检索txt文件中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44771730/

相关文章:

sql - 如何使用来访问SQL Server数据库。通过设置位置的名称

powershell - ForEach-Object 是对管道中的单个对象还是对一组对象进行操作?

powershell - 是否可以自动制作天蓝色弹性泳池秤?

powershell - 在程序文件中使用 exe 调用表达式

powershell - 如何使用 Powershell 停用 Windows 远程桌面?

powershell - 将 Cmdlet 输出与字符串连接起来

powershell - 递归列出网络共享中的文件夹和子文件夹并列出组成员

通过powershell使用/变量进行SQL查询

authentication - Powershell中WebClient的身份验证错误

powershell - 跳转到 PowerShell 脚本中的另一部分