powershell如何从输出中删除`{} @`。有特殊的命令吗?

时间:2014-03-08 04:57:12

标签: powershell wmi

我输入gwmi win32_product | select -property name | select -first 1并输出到文件。我的结果是@{name=Google Talk Plugin}

如何摆脱@{}和名字。我只希望它显示Google Talk Plugin

4 个答案:

答案 0 :(得分:7)

@{}表示您导出具有属性的对象。试试-ExpandProperty中的Select-Object参数。您还可以组合两个select-object命令,例如:

gwmi win32_product | select -expandproperty name -first 1

答案 1 :(得分:1)

I ran into a problem similar with 
$drive = Get-WmiObject Win32_LogicalDisk -ComputerName $servername | Select-Object DeviceID
$drive comes up as @{DeviceID=C:}, @{DeviceID=D:}, ...
Here is my brute force hack at it. 
The second Trim statement was because for some reason if I put it in the first Trim it starts to Trim the letters in the Drive  =D: becomes :

enter code here
$Asdrive = @()   #declared before to get rid of null pointer issue, also to tell PS this is an array not a string

    #Write-Host "Trimming for Get-WmiObject"
for($i=0;$i -lt $drive.length; $i++) { 
 [string]$sdrive = $drive[$i]
 [string]$sdrive1 = $sdrive.Trim("@","{","}","D","e","v","i","c","e","I","D")
 [string]$sdrive2 = $sdrive1.Trim("=")
 $Asdrive += $sdrive2
 }

答案 2 :(得分:0)

如果您至少运行版本3,您还可以使用成员枚举功能,然后使用数组切片来获取第一个,而不是使用select:

(gwmi win32_product).name[0] 

答案 3 :(得分:0)

我添加了一些代码,因为我在谷歌发现了这个问题。

Frode F.解决方案是最好的解决方案。

如果你写出类似的东西:

Get-ADComputer -Filter * -SearchBase $OU | Select-Object Name

您可以获得OU中所有计算机的正确列表。你也可以将它传递给一个CVS / HTML文件,它仍然很好。

| Export-CSV "mylist.csv"

但是如果将它存储到变量(数组)中,每个名称都将包装在@ {}。

在我的情况下,我需要变量中的计算机名称。感谢Frodo,这是解决方案:

$computerList = Get-ADComputer -Filter * -SearchBase $OU | Select-Object -ExpandProperty Name 

希望它有所帮助。 (会在正确的解决方案下将其添加为评论,但我没有足够的声誉这样做)