How to have ConEmu execute a powershell script at startup?

18
2013-11
  • Mike Glenn

    I use the following command to launch new ConEmu powershell consoles.

    ConEmu64.exe /config "shell" /dir "c:\" /cmd powershell -new_console:n
    

    I would like to pass an additional argument to specify a powershell script to run at startup of a new console. The following almost works but only prints the whole command and does not actually execute it:

    ConEmu64.exe /config "shell" /dir "c:\" /cmd 'powershell -noexit -Command {Write-host "Hello world"}' -new_console:n
    

    which produces:

    Write-host Hello world
    C:\>
    

    while I am expecting:

    Hello world
    C:\>
    
  • Answers
  • Maximus

    Remove single quotas around your command. ConEmu executes intact string (command) which follows /cmd switch, with only exception - all -new_console... and -cur_console... are removed from this string before starting console.

    ConEmu64.exe /config "shell" /dir "c:\" /cmd powershell -noexit -Command Write-host "Hello world" -new_console:n
    
  • Mike Glenn

    Ok Figured it out, its a quoting issue, but there is still some weirdness going on:

    /config "shell" /dir "c:\" /cmd 'powershell -noexit -Command "& Write-host `"Hello world""' -new_console:n
    

    That works and produces the expected:

    Hello World
    C:\>
    

    but if you look carefully the quote at the end of "Hello World" is not escaped while the first one is. If I use the seemingly correct syntax:

    /config "shell" /dir "c:\" /cmd 'powershell -noexit -Command "& Write-host `"Hello world`""' -new_console:n
    

    I get

    Hello world`
    C:\>
    

  • Related Question

    How to enable execution of PowerShell scripts?
  • Pavel Chuchuva

    When I try to execute my PowerShell script I get this error:

    File C:\Common\Scripts\hello.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
    At line:1 char:13
    + .\hello.ps1 <<<<
    + CategoryInfo : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException


  • Related Answers
  • Pavel Chuchuva

    Enable running unsigned scripts by entering:

    set-executionpolicy remotesigned
    

    This will allow running unsigned scripts that you write on your local computer and signed scripts from Internet.

    See also Running Windows PowerShell Scripts

  • William Hilsum

    The Default Execution Policy is set to restricted, you can see it by typing:

    Get-ExecutionPolicy

    You should type the following to make it go to unrestricted mode:

    Set-ExecutionPolicy unrestricted

    Hope this helps

  • MDMoore313

    On my machine that I use to dev scripts, I will use -unrestricted as above. When deploying my scripts however, to an end user machine, I will just call powershell with the -executionpolicy switch:

    powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1