PowerShell:将整个文本文件内容存储在变量中 [英] PowerShell: Store Entire Text File Contents in Variable

查看:27
本文介绍了PowerShell:将整个文本文件内容存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 PowerShell 将文本文件的全部内容(包括可能存在也可能不存在的尾随空行)存储在一个变量中.我还想知道文本文件中的总行数.执行此操作的最有效方法是什么?

I'd like to use PowerShell to store the entire contents of a text file (including the trailing blank line that may or may not exist) in a variable. I'd also like to know the total number of lines in the text file. What's the most efficient way to do this?

推荐答案

获取文件的全部内容:

$content = [IO.File]::ReadAllText(".\test.txt")

行数:

([IO.File]::ReadAllLines(".\test.txt")).length

(gc .\test.ps1).length

包含尾随空行的一些hackish:

Sort of hackish to include trailing empty line:

[io.file]::ReadAllText(".\desktop\git-python\test.ps1").split("`n").count

这篇关于PowerShell:将整个文本文件内容存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆