Nicola Cossu
Nicola Cossu

Reputation: 56357

retrieving cpu speed and remove } from output

I'm trying to get cpu speed.

This is what I've done so far

$cpu = [string](get-wmiobject Win32_Processor | select name)
$($cpu.split("@")[-1]).trim()

and my output is

2.40GHz}

How can I remove "}" from my output without having to play with string functions? Is there a better way to achieve my goal? Thanks in advance

Upvotes: 1

Views: 3310

Answers (3)

JPBlanc
JPBlanc

Reputation: 72630

You know what ... I'am Unhappy !

Powershell gives objects ! objects contains informations, and guys you are still trying to manipulate strings

(get-wmiobject Win32_Processor).MaxClockSpeed

Gives the max CPU

After that you can give the string format you want

$cpuSpeed = ((get-wmiobject Win32_Processor).MaxClockSpeed)/1000
$cpuspeedstring = ("{0}Go" -f  $cpuspeed)

Upvotes: 3

Shay Levy
Shay Levy

Reputation: 126732

PS > $p = Get-WmiObject Win32_Processor | Select-Object -ExpandProperty Name
PS > $p -replace '^.+@\s'
2.40GHz

Upvotes: 4

Joel B Fant
Joel B Fant

Reputation: 24766

split() and trim() are string functions, by the way.

You can replace }:

$($cpu.split("@")[-1]).trim() -replace '}',''

Addendum: Here's a simpler way.

$cpu = (get-wmiobject Win32_Processor).name.split(' ')[-1]

The } you were seeing was an artifact produced by casting the results of Select-Object (which creates an object) to a string. Instead you just take the name property directly, split on the space character instead and take the last segment of the string[].

Upvotes: 0

Related Questions