我正在尝试编写一个 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/