1

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.

cloud314
  • 11
  • 1
  • 2
  • 1
    Are you trying this from a PowerShell prompt?Why do your lines start with the word powershell? – Daniel Aug 30 '22 at 17:10
  • Its only 1 line nothing more. I have tried from powershell prompt and as well cmd....both produce the same error. Any ideas? – cloud314 Aug 30 '22 at 17:14
  • You can put brackets around the code to make it a code block `powershell {$stringAssign='random string here'}` – uSlackr Aug 30 '22 at 17:18
  • Putting brackets does work, but I am still perplexed why something so simple is giving off this error. – cloud314 Aug 30 '22 at 17:19
  • 3
    It's because when you're running this command from PowerShell the variables are being expanded before the command is run. So for example, your first command is actually only `= 'random string here'` because $stringAssign gets expanded to its value of nothing – Daniel Aug 30 '22 at 17:23
  • Run that same command without the word PowerShell in the front and it should work without any error – Daniel Aug 30 '22 at 17:24
  • 1
    Thanks that makes sense, and as well explains why wrapping the script in brackets would work. – cloud314 Aug 30 '22 at 17:25
  • 2
    It works fine from cmd. – js2010 Aug 30 '22 at 17:42

1 Answers1

2

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775