powershell how to remove `{}@` from output. Is there a special command to do it?

萝らか妹 提交于 2019-12-24 05:05:36

问题


I entered gwmi win32_product | select -property name | select -first 1 and output to a file. My result was @{name=Google Talk Plugin}.

How can I get rid of @{}, and name. I only want it to show Google Talk Plugin?


回答1:


@{} means your exporting an object with properties. Try the -ExpandProperty parameter in Select-Object. You could also combine both select-object commands, like:

gwmi win32_product | select -expandproperty name -first 1



回答2:


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
 }



回答3:


If you're running at least Version 3, you can also use the member enumeration feature and then array slicing to take the first one, instead of using select:

(gwmi win32_product).name[0] 



回答4:


I add some code as I found this question with google.

Frode F. solution is the best one.

If you write out something like:

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

you get a proper List of all Computers in an OU. You can also pipe that to a CVS/HTML file and its still nice.

| Export-CSV "mylist.csv"

But if you store it into a variable (array) every name will be wrapped in @{}.

In my case I needed computer names in a variable. Here is the solution thanks to Frodo:

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

Hope it helps someone. (would add it as comment under the right solution, but I don't have enough reputation to do so)



来源:https://stackoverflow.com/questions/22265234/powershell-how-to-remove-from-output-is-there-a-special-command-to-do-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!