Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert object to string with custom format

Tags:

powershell

Let's say I have this:

[System.Version]::new(1, 2, 3, 4)

How can I convert it into "3.4"? I'm interested in piping it if possible to avoid having to create an intermediary variable.

like image 280
Chris Long Avatar asked May 14 '26 16:05

Chris Long


2 Answers

this is ugly but it works inline fairly well. it uses the .Split() option to put the results into a specific number of items with all the remaining ones added to the last one.

([version]'1.2.3.4').ToString().Split('.', 3)[-1]

output = 3.4

like image 72
Lee_Dailey Avatar answered May 18 '26 13:05

Lee_Dailey


Another ugly but inline solution, uses ForEach-Object to get at the piped reference.

[System.Version]::new(1, 2, 3, 4) | ForEach-Object { "$($_.Build).$($_.Revision)" }
like image 40
Osvaldo Rosado Avatar answered May 18 '26 12:05

Osvaldo Rosado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!