How can I convert a string to a number using powershell?

How can I convert this text string    4.296 MB (4,504,293 bytes)    to the number  4,504,293  ?


thank you
cyberleo2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
There might be better ways depending on where this is coming from, but with just the information given, this will work.
"4.296 MB (4,504,293 bytes)" -match "\(([0-9,]+) bytes\)$"
[int]$Matches[1]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
to the number  4,504,293
The footech solution will convert the string into a numeric value.  Do you want that conversion or should it remain a string with the embedded commas?

Here is a slightly different pattern that ignores everything after the numeric value, not just " bytes"
"4.296 MB (4,504,293 bytes)" -match "\(([0-9,]+) [^)]+\)$"
$matches[1]

Open in new window

0
cyberleo2000Author Commented:
I don't quite understand how it works, but it does, thank you
0
footechCommented:
Basically it looks at your source string and tries to match it against a regular expression (regex).  When a match is found (the comparison using the -match operator evaluates to true), then the $Matches automatic variable is populated.  The regex includes a capturing group which is what is referenced by $Matches[1] ($Matches[0] includes the entire match), and [int] casts the string in $Matches[1] as an integer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.