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:
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”:
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:
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:
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:
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:
Invoke-Expression $awsCommand
Method 2: Use “&” Sign
Another method to use AWS CLI commands is by adding “&” before the AWS command:
Use “&” sign with S3 command to get the list of buckets:
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.