AWS

How Do I Run AWS CLI Commands in PowerShell Script?

AWS Command Line Interface (CLI) is an open-source tool that allows the user to manage AWS resources using commands. The user can run these commands using PowerShell Scripts on its local system and manipulate AWS resources.

This post will explain the following methods to run AWS CLI Commands in PowerShell Script:

How to Run AWS CLI Commands in PowerShell Script?

Use the following steps to learn how to run AWS CLI commands in PowerShell Script.

Prerequisite: Install AWS Tool in PowerShell.

Search the PowerShell and click on the “Run ISE as Administrator” option:

Verify that the AWS CLI is installed by typing the following command:

aws --version

 

The console displays “aws-cli/2.0.30” that is the installed version of our AWS CLI:

Before installing the AWS tools in PowerShell, allow it to load the Packages for AWS tools by changing the “Execution Policy”:

Set-ExecutionPolicy RemoteSigned

 

Executing the above command will prompt the user to choose one of the following options to change policies:

After getting the permission, install the AWS PowerShell tools package:

Install-Module -Name AWSPowerShell.NetCore

 

Running the above command will prompt the user to allow the installation, as it uses an untrusted repository to install:

Wait for the installation to be complete:

Verify that the AWS tools have been installed:

Get-Module AWS.Tools.Installer -ListAvailable

 

As you can see, the given command displays the details of installed tools with their respective version:

Once the prerequisites are fulfilled, use AWS CLI commands in the PowerShell script.

Method 1: Use “Invoke-Expression” Command

Once the AWS Tools for PowerShell are installed, use “Invoke-Expression” after executing the AWS CLI command as mentioned below:

$awsCommand = "aws ec2 describe-instances"
Invoke-Expression $awsCommand

 

Running the above command displays the details of the EC2 instance:

Use “Invoke-Expression” with the S3 command to get the list of all the buckets:

$awsCommand = "aws s3 ls"
Invoke-Expression $awsCommand

 

Method 2: Use “&” Sign

Another method to use AWS CLI commands is by adding “&” before the AWS command:

& aws ec2 describe-instance

 

Use “&” sign with S3 command to get the list of buckets:

& aws s3 ls

 

It can be observed that the list of the buckets has been displayed on the PowerShell console.

Conclusion

To run AWS CLI commands on PowerShell Script, install AWS CLI on the system to use its commands and also install the AWS tools for PowerShell. AWS CLI commands can be used in PowerShell by either using the “Invoke-Expression” command or adding the “&” sign before the command. The guide explained both these methods with examples.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.