1

Consider this statement:

Get-Content .\zipper.ps1 | Invoke-Expression

Here's a variation documented in Technet.

Invoke-Expression (Get-Content '.\zipper.ps1' -Raw)

Both statements work just fine, although the 2nd one executes much faster. Assume zipper.ps1 is a script containing multiple lines of executable Powershell code.

Will the above statement execute each line in zipper.ps1 as an individual command?

Or, will the entire script get executed as just one big concatenated string?

My question pertains to character limits for powershell statements-- if each code-line is within the powershell character-limit, but the entire script would exceed the character limit, will the above command exceed the character limit?

PowerShell may have a 260-character limit (not sure). These pages mention character limits in Powershell:

https://support.microsoft.com/en-us/kb/830473

https://social.technet.microsoft.com/Forums/windowsserver/en-US/f895d766-5ffb-483f-97bc-19ac446da9f8/powershell-command-size-limit?forum=winserverpowershell


Details:

Technet states:

"Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file."

and

"Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command."

So far so good-- that explains pretty clearly how and why my statement above works. But it does not tell me if the statements are executed as individual statements, or as one big concatenated string. The fact that the 2nd variation runs faster may be a clue.

Maybe I need to add a For..Each.

The technet doc says Get-Content returns a collection, while this Technet article says it returns an array. That's conflicting info, but not sure array vs collection matters re my question here.

thx

Cole9350
  • 5,444
  • 2
  • 34
  • 50
johny why
  • 2,047
  • 7
  • 27
  • 52
  • if you're question for every line in your content this Invoke-Expression will check all of statements . – saftargholi Oct 03 '16 at 13:54
  • Indeed each line is evaluated separately when piping is used: look at the iex's parameter description in the docs: it accepts `pipeline input`. As for character limits - why do you think it's an issue? This is the first time I hear this concern regarding PS scripts. – wOxxOm Oct 03 '16 at 13:57
  • Thx for reply, @wOxxOm. Can you document that each line is evaluated separately when piping is used? – johny why Oct 03 '16 at 14:00
  • the iex does indeed accept piping. So, you are asserting that piping always evaluates statements 1-line at a time? (also, links added to OP re character limits) – johny why Oct 03 '16 at 14:07
  • a thorough and awesome answer was unfortunately removed. @wOxxOm, hope you will repost. thx for responding to my previous comments – johny why Oct 04 '16 at 19:12

0 Answers0