I'm trying to write some powershell scripts to "one-click" some setup for project initialisation.
I am unable to get it to call the activate script. Typically, in a terminal I would create the virtual environment, then call the activate script using .\venv\Scripts\Activate
. However, because I want to have the script be flexible, I am letting (forcing) users to pick a name for their virtual environment.
Onto the script:
function Make-Python-Environment {
[CmdletBinding()] #make an advance function
Param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$VirtualEnvName,
[Parameter(Mandatory)]
[TransformBooleanInputs()] # not relevant to this, just parses string to boolean t/f
[switch]$IncludeDotEnv = $False
)
begin {
Write-Host "This is only a test"
}
end {
$ThisDir = (Get-Item .).FullName
if($IncludeDotEnv){
New-Item ".env" -Force
Write-Host ".env file has been created in the current directory."
} else {
Write-Host "Skipping .env creation."
}
if($VirtualEnvName){
py -m venv $VirtualEnvName
Write-Host "Created virtual environment at" (Join-Path -Path $ThisDir -ChildPath $VirtualEnvName)
.\$VirtualEnvName\Scripts\Activate #<-- point of failure
}
}
}
This is the output when I run the script:
PS C:\Users\Alan> Make-Python-Environment
cmdlet Make-Python-Environment at command pipeline position 1
Supply values for the following parameters:
VirtualEnvName: fa
IncludeDotEnv: 1
This is only a test
Directory: C:\Users\Alan
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 09/01/2025 13:41 0 .env
.env file has been created in the current directory.
Created virtual environment at C:\Users\Alan\fa
.\$VirtualEnvName\Scripts\Activate : The term '.\$VirtualEnvName\Scripts\Activate' 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 C:\Users\Alan\Documents\PythonSetup.ps1:54 char:9
+ .\$VirtualEnvName\Scripts\Activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\$VirtualEnvName\Scripts\Activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have also tried wrapping the the VirtualEnvName in parenthesis .\($virtualEnvName)\Scripts\Activate
but that also fails.
What exactly is .\$VirtualEnvName\Scripts\Activate
and what are you expecting it to do?
From the context it sounds like it's a Python script perhaps, without a file extension? If so then you surely need to call Python itself and pass it that information, eg :
python .\$VirtualEnvName\Scripts\Activate
especially since without a file extension there's no way for the system to know what that file is.
Alternatively, while I've not tried calling Python scripts from PowerShell, when calling external PowerShell scripts you'd need to do something like :
& C:\path\myscript.ps1
or
& .\myscript.ps1
not just give it the file path and expect PowerShell to work out what to do with it.