3

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?

2
  • 1
    I don't get that. PS 4.0. Commented Mar 8, 2014 at 5:04
  • Not that great with PS, so just a comment, but did you try switching the order of the selects? Limit to the first 1 result first, and then project to your name property, instead of the other way around? Perhaps with -ExpandProperty? Commented Mar 8, 2014 at 5:39

4 Answers 4

14

@{} 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
Sign up to request clarification or add additional context in comments.

Comments

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
 }

7 Comments

That's a lot of code for something that can simply be done bij surrounding the command in parentheses and then typeing a dot followed by the propertyname, so (ger-wmiobject blabla).DeviceId. see the answer of mjolinor.
Thank you, like I said it is a hack job. If you can clean it up I would be appreciative and more than willing to learn.
I am not sure I understand how to use member enumeration with array slicing. I tried $driveTest = (gwmi win_32product).name[0] as well as (gwmi win_32product).name[0] and they both just cause the program to hang.
Win32_product is broken and should be avoided at all costs since it forces all msi packages to reregister (that's also what makes it so slow). Even Microsoft has documentation on it explaining it (and a workaround)
Ok then I am lost I guess. What is the proper way to do this? Does this mean that Get-WmiObject is broken too and they are both supposed to only contain the contents and not the @{ } wrapper ?
|
0

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] 

1 Comment

If you have only one win32_product then .name[0] will refer to the first char of that product name.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.