1

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

Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98

3 Answers3

4
PS > $p = Get-WmiObject Win32_Processor | Select-Object -ExpandProperty Name
PS > $p -replace '^.+@\s'
2.40GHz
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
3

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)
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • And I would still parse the string. On one of my machines, `MaxClockSpeed` is 1862, which is close enough to the "`1.86GHz`" in the `Name`. But on another, `MaxClockSpeed` is 1994, and I'd rather display "`2.00GHz`" from `Name`. If it has to be formatted to a string in the end anyway for displaying, I'd rather start with a string that's already formatted the way I want and take a chunk out of it rather than tweak an integer that's a bit *too* accurate. – Joel B Fant Jul 12 '11 at 16:10
0

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[].

Joel B Fant
  • 24,406
  • 4
  • 66
  • 67
  • +1. Hi Joel. Thanks for your quick reply. I know that split() and trim() are both string functions but it seems to me very strange all this mess to get a simple information and I hope there is a cleaner way. :) – Nicola Cossu Jul 11 '11 at 22:17
  • Downvoter: I'd appreciate an explanation how my answer is either not useful or incorrect. (It can't possibly be retaliatory, eh? I've downvoted no answer here.) – Joel B Fant Jul 12 '11 at 16:43
  • 1
    Joel. Don't mind. Sometimes it happened even to me to be downvoted even for accepted answers with many votes. It's a strange world. :) Even my question has been downvoted, probably from the same guy. We'll survive the same. – Nicola Cossu Jul 12 '11 at 19:55
  • @nick: I know. It's just that the lack of explanation isn't constructive. That's why we can change our vote if a post has been edited. – Joel B Fant Jul 12 '11 at 20:35