2

I'm a beginner at Powershell and am struggling to understand some syntax from some code I found on Github. I've read the docs on Powershell assignment, and on switch statements, and can't understand what is going on with the = $Yes and = $No in this code snippet:

Switch ($Prompt3) {
  Yes {
         Stop-EdgePDF
         Write-Output "Edge will no longer take over as the default PDF viewer."; = $Yes
   }
  No   {
         = $No
   }
}

I haven't been able to find any references to this kind of syntax, and it doesn't seem to do anything in the script. So why is it there?

2
  • 3
    = is being overloaded as a function or alias. There is code missing from your example. Observe: ${function:=} = { $args }; = 'test' or function = { $args } is another syntax Commented Nov 21, 2018 at 22:58
  • 1
    The issue has been resolved in the latest version of the script. See github.com/Sycnex/Windows10Debloater/issues/66 Voting to close because this was a typographical error. Commented Dec 3, 2018 at 17:11

2 Answers 2

1

UPDATE: This issue has been resolved.


Looks to me like the variable name that was getting the assignment was deleted in a change back in August.

$PublishSettings = $Yes

Was changed to:

= $Yes

And:

$PublishSettings = $No

Was changed to:

= $No

Looks like poor search and replace.

I've created an issue for the problem at GitHub.

Sign up to request clarification or add additional context in comments.

Comments

1

There are many characters that are valid in a function (or variable) name; this includes the = symbol. What you're observing is a function or alias.

Examples:

# standard function
function =
{
    return $args
}

# accessing the function: drive
${Function:=} = {
    return $args
}

# defining a new alias
New-Alias -Name = -Value Get-Variable

# using the Alias attribute
function Test-Thing
{
    [Alias('=')]
    param()

    return $args
}

9 Comments

Thank you for your answer. I've read it several times but still having a hard time wrapping my head around it. You're right that I didn't include all the code, but one other detail to mention is that the variables (aliases?) $Yes and $No are not used any where else in the PS script, which includes several Functions followed by several Switches based on user prompts like the one shown.
@jacobsee I'm assuming there's something that looks like $Yes = switch(condition) { somewhere in the codebase. If the variable is undefined, it returns $null
There is not. But if there were, what would that do? In my example, what is being assigned to the $No variable? It's an empty pair of brackets!? Thanks for your patience.
@jacobsee That's where your misunderstanding is coming - = is not an assignment operator in your code, it's a function call. Someone has literally defined a function named =. { } is an empty scriptblock being passed to the = function. You can assign expression outputs to variables, such as: $myvar = if ($true) { 'thisval' } else { 'thatval' }
@jacobsee Yeah, I reviewed the project and there's no definition for a = function or alias so that line of code would throw an exception.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.