skip to Main Content

I have a PowerShell script conda-init.ps1 that activates Conda via this single line:

(& "pathtocondaScriptsconda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression

I currently launch PowerShell using the arguments -noexit & pathtoconda-init.ps1.

This does not work on machines where the PowerShell ExecutionPolicy is set to Restricted. Is there a way to instead pass the script contents as arguments to PowerShell, rather than the script file itself?

The end goal is to construct a VS Code terminal profile that mimics the behavior of the Anaconda prompt avoid having to pass a script file (as above) or running conda init powershell (equivalent to passing a file, but makes it the default behavior of all Powershell prompts and still requires a non-restricted ExecutionPolicy).

2

Answers


  1. Chosen as BEST ANSWER

    The Miniconda prompt created during Miniconda install runs using the command

    %windir%System32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'pathtominicondashellcondabinconda-hook.ps1' ; conda activate 'pathtominiconda' "
    

    Which does use the -ExecutionPolicy Bypass command described by @maximilian-burszley. I can create an equivalent VS Code terminal profile in settings.json by adding the following entry to terminal.integrated.profiles.windows:

    "Conda": {
        "source": "PowerShell",
        "icon": "terminal-powershell",
        "overrideName": true,            
        "args": ["-ExecutionPolicy",  "ByPass", "-NoExit", "-Command",  "& 'path\tominiconda\shell\condabin\conda-hook.ps1' ; conda activate 'path\to\miniconda' "]
    }
    

    But it remains to be seen if this will work for users without admin permissions, as my experience is that non-admin users cannot bypass their execution policy.


  2. This doesn’t need to be a powershell script at all. You can use a batch script:

    conda-init.bat

    powershell.exe -ExecutionPolicy Bypass -Command "& 'pathtocondaScriptsconda.exe' shell.powershell hook"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search