1

I have to run my powershell scripts ONLY with these way:

Get-Content "ScriptName.ps1" -raw | powershell.exe -ExecutionPolicy byPass -NoExit -Command -

It works ok with some scripts but others cannot be executed with these way, even if they work from PowerShell ISE, or Visual Studio code. Executing ends without any output or warnings.

For me, it looks like when we run raw text as a Command for powershell.exe some symbols or commands don't work the same way as we run .ps1 script from ISE or VS code. I cannot find the rules for Execution scripts like these. Could someone help me with the rules, what i need to do with any script before i wanna run it like these?

I have tried escaping special characters with "/" but it doesnt work

5
  • Try setting WorkingDirectory. The script may need to be run from a specific folder. See : learn.microsoft.com/en-us/powershell/module/… Commented Jan 24, 2023 at 11:14
  • Ty for your answer, but specifying WorkDirectory didn't help =( Commented Jan 24, 2023 at 11:29
  • "I have to run my powershell scripts ONLY with these way" - why? What's the purpose? Commented Jan 24, 2023 at 11:53
  • 1
    I assume this is because of a domain policy in place - why not ask your domain admin to create a group policy which allows unsigned scripts for certain users? Commented Jan 24, 2023 at 12:12
  • Im writing scripts for big system which, unfortunately, can execute it only in way like these. I found only one rule for these way of executing: after "}" always have to be a space or new string, but it's still not enough, coz still not all scripts can be executed. Help for "powershell.exe" command is : " If the value of Command is "-", the command text is read from standard input." But nothing about format of that input. I'm sure, that there have to be more complex instruction, but i can't find nothing more about Commented Jan 24, 2023 at 13:47

2 Answers 2

2

PowerShell's execution policy is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts.

Script editors run PS commands as if you typed them into the console, therefore don't trigger the execution policy in most cases.

Rather than trying to bypass the execution policy this way you should either sign your scripts or put in place a domain policy which allows the script to be run normally.

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

Comments

2

To add to Scepticalist's helpful answer:

  • If a GPO (Group Policy Object) on your system prevents execution of your scripts, -ExecutionPolicy Bypass will not be able to override that.

  • Without a GPO in place, running e.g. Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force as a one-time action should suffice, after which you'll be able to run (local) scripts directly in the context of your account.


In other words: if a GPO is the problem, -ExecutionPolicy Bypass will have no effect, and executing your script's content as a workaround will only succeed if its code doesn't try to call other scripts or script-based modules / modules with configuration data - see this answer for a comprehensive overview of PowerShell's execution policies.

A solution to your problem:

  • If you want to run your script's code in a child process (this assumes that you're calling from PowerShell, because only then is passing a script block supported - see this answer for background:

    powershell.exe ([scriptblock]::Create((Get-Content ScriptName.ps1 -Raw)))
    
    • To also pass - invariably positional - arguments, use -args with an array of arguments (e.g. ... -args foo, 42)
  • If you want to run it in-process (note that Invoke-Expression is otherwise best avoided):

    Invoke-Expression (Get-Content ScriptName.ps1 -Raw)
    
    • To also pass - potentially also named - arguments, you again need a script block; pass arguments individually, as usual:

      & ([scriptblock]::Create((Get-Content ScriptName.ps1 -Raw))) foo 42
      
    • The caveat in both these in-process calls is that if your script file's code terminates with exit, the calling session will exit too.


As for what you tried:

  • Using powershell.exe, the Windows PowerShell CLI, or pwsh.exe, the PowerShell (Core) CLI, with -Command - has quirks and limitations, summarized in GitHub issue #3223.

  • In short, it exhibits pseudo-interactive behavior, runs each statement in its own pipeline, and requires two newlines at the end of the input in order to ensure that multiline statements are reliably executed.

Comments

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.