> ## Documentation Index
> Fetch the complete documentation index at: https://tally.wharflab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# powershell/PSPossibleIncorrectUsageOfAssignmentOperator

> Equal sign is not an assignment operator. Did you mean the equality operator \'-eq\'?

`powershell/PSPossibleIncorrectUsageOfAssignmentOperator` is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in
Dockerfiles.

| Property | Value            |
| -------- | ---------------- |
| Severity | Information      |
| Category | PSScriptAnalyzer |
| Auto-fix | No               |

## Description

In many programming languages, the equality operator is denoted as `==` or `=`, but `PowerShell`
uses `-eq`. Therefore, it can easily happen that the wrong operator is used unintentionally. This
rule catches a few special cases where the likelihood of that is quite high.

The rule looks for usages of `==` and `=` operators inside `if`, `else if`, `while` and `do-while`
statements but it does not warn if any kind of command or expression is used at the right hand side
as this is probably by design.

## Examples

### Problematic code

```powershell theme={null}
if ($a = $b)
{
    ...
}
```

```powershell theme={null}
if ($a == $b)
{

}
```

### Correct code

```powershell theme={null}
if ($a -eq $b) # Compare $a with $b
{
    ...
}
```

```powershell theme={null}
if ($a = Get-Something) # Only execute action if command returns something and assign result to variable
{
    Do-SomethingWith $a
}
```

## Implicit suppression using Clang style

There are some rare cases where assignment of variable inside an `if` statement is by design.
Instead of suppressing the rule, one can also signal that assignment was intentional by wrapping the
expression in extra parenthesis. An exception for this is when `$null` is used on the LHS because
there is no use case for this.

```powershell theme={null}
if (($shortVariableName = $SuperLongVariableName['SpecialItem']['AnotherItem']))
{
    ...
}
```

## Source

This rule documentation is adapted from Microsoft's PSScriptAnalyzer documentation for
[PossibleIncorrectUsageOfAssignmentOperator](https://github.com/MicrosoftDocs/PowerShell-Docs-Modules/blob/main/reference/docs-conceptual/PSScriptAnalyzer/Rules/PossibleIncorrectUsageOfAssignmentOperator.md),
licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
