powershell command not running in powershell script

When I run the below command manually it works fine but does not work when I use in PowerShell script.

 

powershell.exe -ExecutionPolicy RemoteSigned -noprofile -Noninteractive {$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('~'); powershell -ExecutionPolicy ByPass -File "C:\myscript.ps1"}
  • Virtually anything you run manually, in the PowerShell ISE or type and the console host is expected to run, since it's an interactive session, not a script.

    The moment you pop it all into a script, then execution policy and permissions take the drivers seat.

    Why are you changing the execution policy twice?

    The first command, says the script must be signed (when used remotely), thus our script will not run until you sign it.

    'technet.microsoft.com/heyscriptingguy/2010/06/17/hey-scripting-guy-how-can-i-sign-windows-powershell-scripts-with-an-enterprise-windows-pki-part-2-of-2'

    Unrestricted No requirements; all scripts allowed
    RemoteSigned All local scripts allowed; only signed remote scripts
    AllSigned All scripts need to be signed
    Restricted No scripts allowed

    By default the PoSH execution policy is set to RemoteSigned out of the box. So, no real reason to call it again in a script, unless you or your Org change that.