cloud314
cloud314

Reputation: 11

Powershell - The term '=' is not recognized as the name of a cmdlet

Hello everyone I have a problem with powershell if anyone could explain why I am receiving an error it would be greatly appreciated.

Trying to assign a simple string...

powershell $stringAssign = 'random string here'

= : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Also tried putting it inside of a function, but it shows different error this time and a command I didn't even run...

powershell function DoStuff{$stringAssign = '3c68746d6c20786d6c6e733d22687474703a2f2f777777'}

At line:1 char:17 function DoStuff -encodedCommand JABzAHQAcgBpAG4AZwBBAHMAcwBpAGcAbgAg ... ~ Missing function body in function declaration.

Please anyone that can explain what is going on here and why an error is happening when trying to assign a simple string, it would be greatly appreciated.

Upvotes: 1

Views: 20776

Answers (2)

Tomas Kubes
Tomas Kubes

Reputation: 25128

I got the same error message in different code, which leads me to this page.

In my case I was using C3 syntax in if

if($foo == $false)

instead of Powershell syntax

if($foo -eq $false)

Upvotes: 0

mklement0
mklement0

Reputation: 438008

To build on the helpful comments:

  • You're calling the Windows PowerShell CLI, powershell.exe, i.e. you're creating a child process to which you pass command-line arguments.

  • You must first satisfy the calling shell's syntax requirements to ensure that the arguments are passed as intended, which typically involves a quoting and /or escaping:

    • From cmd.exe, your command would work as-is (but do nothing useful), because none of the characters in $stringAssign = 'random string here' are subject to up-front interpretation by cmd.exe. However, it is safer to enclose the PowerShell command in "...", because you then needn't worry about characters that cmd.exe does interpret itself if unquoted, such as &, |, and >:

      :: From cmd.exe      
      powershell "$stringAssign = 'random string here'; $stringAssign"
      
      • ; is the statement separator, and $stringAssign simply outputs (echoes) the variable value, to demonstrate that the assignment worked.

      • If the command itself contains ", escape them as \" or - in edge cases - as "^"" (sic; with pwsh.exe, the PowerShell (Core) 7+ CLI, use "") - see this answer.

    • From PowerShell, your command fails for the reasons explained by Daniel:

      • $stringAssign is expanded up front in the calling session, and since no such variable exists yet, the command seen by the powershell.exe child process executes = "random string here", which causes the error you saw.

      • The immediate fix is to enclose the command in verbatim (single-quoted) string ('...'), so that no up-front expansion is performed; embedding literal ' inside '...' requires escaping them as '':

        powershell ' $stringAssign = ''random string here''; $stringAssign '
        
        • To include variable values from the caller's scope, use an expandable (double-quoted) string ("...") with appropriate escaping with ` of the $ in those variable reference that should not be expanded up front; e.g.:

          powershell "`$stringAssign = '$HOME'; $stringAssign; `$stringAssign"
          
      • Note that there is rarely a need to call the PowerShell CLI from inside PowerShell, because in-process execution is much faster than creating another PowerShell instance as a child process, but when you do, the best solution is to enclose the command in { ... }, i.e. a script block, but note that this works only from PowerShell.

        # Works from PowerShell only.
        powershell { $stringAssign = 'random string here'; $stringAssign }
        
        • To include variable values from the caller's scope, you must pass them as arguments, which in the simplest case you can access via the automatic $args variable:

          # Works from PowerShell only.
          powershell { $stringAssign = $args[0]; $stringAssign } -args $HOME
          

For a comprehensive overview of PowerShell's CLI, see this post.

Upvotes: 3

Related Questions