How to have ConEmu execute a powershell script at startup?
2013-11
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:\>
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
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:\>
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
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
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
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