Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment a variable in PowerShell in functions

Tags:

powershell

How do I increment a variable in a PowerShell function?

I'm using the below example without any data to be input to the function. I want to increment a variable every time a function is called. The variable $incre has 1 added to it and then displays the total of $incre when the script completes.

The total when running the below is 0, whereas the result I want is 4 as the function comparethis has been run 4 times and each time $incre has been incremented by 1.

 $incre = 0

 function comparethis() {
     # Do this comparison

    $incre++
    Write-Host $incre
 }

 comparethis #compare 2 variables
 comparethis #compare 2 variables
 comparethis #compare 2 variables
 comparethis #compare 2 variables

 Write-Host "This is the total $incre"
like image 645
user3175140 Avatar asked Jul 09 '14 03:07

user3175140


People also ask

What does += in PowerShell mean?

The assignment by addition operator += either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection).

How do you increment variables?

The most simple way to increment/decrement a variable is by using the + and - operators. This method allows you increment/decrement the variable by any value you want.

What does $_ do in PowerShell?

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.


3 Answers

You are running into a dynamic scoping issue. See about_scopes. Inside the function $incre is not defined so it is copied from the global scope. The global $incre is not modified. If you wish to modify it you can do the following.

$incre = 0

function comparethis() {
    #Do this comparison

    $global:incre++
    Write-Host $global:incre
}

comparethis #compare 2 variables
comparethis #compare 2 variables
comparethis #compare 2 variables
comparethis #compare 2 variables

Write-Host "This is the total $incre"
like image 109
StephenP Avatar answered Oct 08 '22 08:10

StephenP


If you want your counter to reset each time you execute the same script use $script scope:

$counter = 1;

function Next() {
    Write-Host $script:counter

    $script:counter++
}

Next # 1
Next # 2
Next # 3

With $global scope you will get 4 5 6 on the second script run, not 1 2 3.

like image 44
Aleksei Mialkin Avatar answered Oct 08 '22 06:10

Aleksei Mialkin


Rather than using a global variable I woul suggest to call the function with a reference to the variable:

[int]$incre = 0

function comparethis([ref]$incre) {
    $incre.value++;
}

comparethis([ref]$incre) #compare 2 variables
Write-Host $incre
comparethis([ref]$incre) #compare 2 variables
Write-Host $incre
like image 1
Tyron78 Avatar answered Oct 08 '22 07:10

Tyron78