1

I want to create an alias in my Profile.ps1 running a bunch of commands. I have separated each command with a semicolon:

New-Alias -Name venv -Value 'echo "venv" >> .gitignore ; python3 -m venv --copies venv ; venv\scripts\activate.ps1 && pip install -U pip pylint black pep8 pydocstyle ; pip list ; python --version'  # Create virtual environment "venv"

However, this errors out with the following:

venv : The module 'echo "venv" >> .gitignore ; python3 -m venv --copies venv ; venv' could not be loaded. For more information, run 'Import-Module echo "venv" >> .gitignore ; python3 -m venv --copies venv ; venv'.
At line:1 char:1
+ venv                                                                                                                                            
+ ~~~~
+ CategoryInfo          : ObjectNotFound: (echo "venv" >> .git\u2026 ; python --version:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule

If I simplify the command down to New-Alias -Name venv -Value 'echo "venv" >> .gitignore' I still get an(other) error:

venv : The term 'echo "venv" >> .gitignore' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ venv
+ ~~~~
+ CategoryInfo          : ObjectNotFound: (echo "venv" >> .gitignore:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

What am I doing wrong here?

fredrik
  • 9,631
  • 16
  • 72
  • 132

1 Answers1

2

You should use a function if you have multiple commands:

function venv { 
    echo "venv" >> .gitignore; 
    python3 -m 
    ...etc...
}
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • Indeed, a function is necessary, but note that even a _single_ command cannot generally be defined as an alias - unless it happens to be argument-less. To put it differently: PowerShell aliases can only map a name to another _name_ (or executable _file path_). – mklement0 May 25 '19 at 11:54