user3395201
user3395201

Reputation: 31

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

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?

Upvotes: 3

Views: 30158

Answers (4)

Daniel H
Daniel H

Reputation: 35

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)

Upvotes: 0

Skylark
Skylark

Reputation: 13

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
 }

Upvotes: 1

mjolinor
mjolinor

Reputation: 68331

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] 

Upvotes: 0

Frode F.
Frode F.

Reputation: 54951

@{} 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

Upvotes: 14

Related Questions