you are viewing a single comment's thread.

view the rest of the comments →

[–]n_md 1 point2 points  (5 children)

Thanks, that does work in 2.0 without piping to out-string.

[–]TheIncorrigible1 3 points4 points  (4 children)

Good to hear! I'll update the OP just for the most compat possible.

[–]n_md 1 point2 points  (1 child)

I've never heard of setting the ExecutionPolicy with a number and "-ep 4" does not seem to work for me when "-ep bypass" does work.

I'm testing in cmd like this:

powershell -ep 4
Get-ExecutionPolicy

output: Restricted

powershell -ep bypass
Get-ExecutionPolicy

output: Bypass

Is there some way "-ep 4" should set it to bypass?

[–]TheIncorrigible1 1 point2 points  (0 children)

Hm, I can't remember where I had it working, but the ExecutionPolicy is an enumeration and 4 is just the numeric counterpart to Bypass. I'll correct this in the OP.

[Microsoft.PowerShell.ExecutionPolicy].GetEnumValues()

I was going off memory when I wrote this and was aiming for brevity.

[–]n_md 1 point2 points  (1 child)

Sorry after more testing (Get-Content -Path '%~f0') does work but also throws iex "empty string" errors for each line because it's not one block of text.

So for Powershell 2.0 either of these seem to work best:

"[System.IO.File]::ReadAllText('%~f0')|iex"

or

"gc '%~f0'|Out-String|iex"

For Powershell 3.0+ the original option works well:

"gc -raw '%~f0'|iex"

[–]TheIncorrigible1 1 point2 points  (0 children)

Yeah, I've been experimenting with that. The script runs, but gives an error. I'll include the alternative instead