amazon-web-servicespowershell

PowerShell does not recognize AWS CLI installed in the same script


I have installed aws cli using powershell script

 $command = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
 Invoke-Expression $command
 Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -Outfile C:\AWSCLIV2.msi
 $arguments = "/i `"C:\AWSCLIV2.msi`" /quiet"
 Start-Process msiexec.exe -ArgumentList $arguments -Wait
 aws --version

When I try to print the aws --version it gives the below error.

aws : The term 'aws' 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
+ aws
+ ~~~

Solution

  • I was able to fix this by adding the below line after installing aws cli:

    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    

    complete code:

    $command = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
    Invoke-Expression $command
    Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -Outfile C:\AWSCLIV2.msi
    $arguments = "/i `"C:\AWSCLIV2.msi`" /quiet"
    Start-Process msiexec.exe -ArgumentList $arguments -Wait
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    aws --version
    aws s3 ls