Reputation: 44665
I need assistance with a powershell script.
I have a value - 2.0.0.0 (it could be any value seperated with the 3 full stops)
I want to just get the first three numbers eg 2.0.0
With powershell, how can I do this?
Upvotes: 1
Views: 97
Reputation: 16646
A regular expression is a flexible way to do this:
"1.2.3.4" -replace "(.*)\.(.*)\.(.*)\.(.*)",'$1.$2.$3'
or inspired by Shay Levy's answer:
You could use the toString method with a fieldcount of three:
([version]"1.2.3.4").tostring(3)
Upvotes: 2
Reputation: 126902
Here's another way:
PS> '2.0.0.0'.split('.')[0..2] -join '.'
2.0.0
By the way, you can cast it to a system.version object and work with it's properties:
PS> [version]'2.0.0.0'
Major Minor Build Revision
----- ----- ----- --------
2 0 0 0
Upvotes: 4
Reputation: 37800
Nothing at all wrong with the other answers, but just to show more diversity:
[string]::Join('.', '2.0.0.0'.Split('.')[0..2])
Upvotes: 0