如何在powershell中将字符串转换为表格或对象 [英] How to convert string to table or objects in powershell

查看:81
本文介绍了如何在powershell中将字符串转换为表格或对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下导出的文本转换为 csv,以便我可以将其用作 powershell 中的对象.例如:

How can I convert the below exported text to csv so that i can use it as objects in powershell. Eg:

where{$_.qlimit -eq 27}

文字:

  samid            qlimit    qused  
  Administrator    -1        0      
  Guest            -1        0      
  admin            27        8      
  krbtgt           -1        0      
  r                -1        0      
  admin2           44        0  

推荐答案

使用 Get-Content cmdlet 加载文件,用逗号替换两个或多个空格,然后使用 ConvertFrom-Csv cmdlet:

Use the Get-Content cmdlet to load the file, replace two or more whitespaces with a comma and convert it to CSV using the ConvertFrom-Csv cmdlet:

$object = (Get-Content 'your_file').Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv

如果您现在查询您的对象:

If you now query your object:

$object | where qlimit -eq 27

你得到了想要的输出:

samid qlimit qused
----- ------ -----
admin 27     8    

这篇关于如何在powershell中将字符串转换为表格或对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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