1

I would like to set a variable to be used inline as below but it does not behave as I anticipated. Ultimately the command that works that I would like it to run, just use a variable to set it, is

'$Version = 'v14,11.253.0'.replace(",",".").replace("v","")' to get a output of '14.11.253.0'

Why does this not work?

$Replace = '.replace(",",".").replace("v","")'
$Version = 'v14,11.253.0'$Replace
$Version

2 Answers 2

1

Per my comment, here's how you might use a Function to make this code more repeatable:

Function FixVersion ($version) { $version  -replace ',','.' -replace 'v' }

$Version = FixVersion 'v14,11.253.0'

You could also take this a little further (and perhaps make it more usable) by having the function accept pipeline input so that you can use it like this:

Function FixVersion { 
    Param (
        [Parameter(ValueFromPipeline)]
        $Version
    )
    $Version  -replace ',','.' -replace 'v'
 }

$Version = 'v14,11.253.0' | FixVersion
Sign up to request clarification or add additional context in comments.

Comments

0

This should work for you:

$Version = 'v14,11.253.0' -replace ',','.' -replace 'v'

Not sure why you're trying to store the replace methods in a string variable, but it won't behave as you're expecting, because it's a string.

2 Comments

I need to change multiple lines of code with different 'replace' commands so i would like to change it in one location (i.e. set variable) so I can use it over and over again and not have to constantly be changing dozens of lines of code.
It sounds like you should create a function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.