DEV Community

Hilda Ogamba
Hilda Ogamba

Posted on • Edited on

AWS Tools for PowerShell

Introduction

From the AWS documentation:

The AWS Tools for PowerShell are a set of PowerShell cmdlets that are built on top of the functionality exposed by the AWS SDK for .NET. The AWS Tools for PowerShell enable you to script operations on your AWS resources from the PowerShell command line.

Just like the AWS CLI, the AWS Tools for PowerShell let developers and administrators manage their AWS services and resources in the PowerShell scripting environment.

Installation and Configuration (on Windows)

These tools are only available from version on computers that are running Windows with Windows PowerShell 5.1, or PowerShell Core 6.0 or later.
Run the Windows PowerShell as Administrator.
Run the following command to install AWS Tools for PowerShell:

Install-Module -Name AWSPowerShell
Enter fullscreen mode Exit fullscreen mode

Answer "Y" to the interactive prompts.

Next, try to see if the application has installed correctly:

Get-AWSPowerShellVersion
Enter fullscreen mode Exit fullscreen mode

Incase you encounter errors stating that "... the module could not be loaded ..." or that the module "... cannot be loaded because running scripts is disabled on this system ...", you may need to change the Execution Policy.
Run the following command to change the Execution Policy:

Set-ExecutionPolicy RemoteSigned
Enter fullscreen mode Exit fullscreen mode

Answer "Y" to the interactive prompts.

Next, set the proper AWS Credentials. If you don't have an administrator set up for your AWS account, follow the instructions here.
Once the administartor user has been set up, generate access keys for that user and securely store that information.

To add a new profile to the AWS SDK store, run the command (replace the AccessKey and SecretKey codes with the administrator access keys):

Set-AWSCredential `
                 -AccessKey AKIA0123456787EXAMPLE `
                 -SecretKey wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY `
                 -StoreAs Administrator
Enter fullscreen mode Exit fullscreen mode

Next, save the configuration as the default profile and region for every PowerShell session:

Initialize-AWSDefaultConfiguration -ProfileName Administrator -Region us-west-2
Enter fullscreen mode Exit fullscreen mode

AWS Tools for PowerShell Example Cmdlets

Here are some example cmdlets:

Create Key-Pair

$myPSKeyPair = New-EC2KeyPair -KeyName myPSKeyPair
Enter fullscreen mode Exit fullscreen mode

View Key-Pair Details

$myPSKeyPair | Get-Member
Enter fullscreen mode Exit fullscreen mode

Save Key-Pair

$myPSKeyPair.KeyMaterial | Out-File -Encoding ascii myPSKeyPair.pem
Enter fullscreen mode Exit fullscreen mode

Remove Key Pair from AWS

Remove-EC2KeyPair -KeyName myPSKeyPair
Enter fullscreen mode Exit fullscreen mode

Create Security Group

New-EC2SecurityGroup -GroupName myPSSecurityGroup -GroupDescription "EC2-Classic from PowerShell"
Enter fullscreen mode Exit fullscreen mode

View Security Groups

Get-EC2SecurityGroup -GroupNames myPSSecurityGroup
Enter fullscreen mode Exit fullscreen mode

Remove Security Group

Remove-EC2SecurityGroup -GroupId sg-0113e926ee099393f
Enter fullscreen mode Exit fullscreen mode

Launch an instance

New-EC2Instance -ImageId ami-0ca285d4c2cda3300 `
    -MinCount 1 `
    -MaxCount 1 `
    -KeyName myPSKeyPair `
    -SecurityGroups myPSSecurityGroup `
    -InstanceType t2.micro
Enter fullscreen mode Exit fullscreen mode

View Instance

$reservation = New-Object 'collections.generic.list[string]'
$reservation.add("r-b70a0ef1")
$filter_reservation = New-Object Amazon.EC2.Model.Filter -Property @{Name = "reservation-id"; Values = $reservation}
(Get-EC2Instance -Filter $filter_reservation).Instances
Enter fullscreen mode Exit fullscreen mode

Terminate Instance

Remove-EC2Instance -InstanceId i-0453116cdface121d
Enter fullscreen mode Exit fullscreen mode

List Buckets

Get-S3Bucket
Enter fullscreen mode Exit fullscreen mode

Create a bucket

New-S3Bucket -BucketName uniquebucketname2258 -Region us-west-2
Enter fullscreen mode Exit fullscreen mode

Upload an object to the bucket

Write-S3Object -BucketName uniquebucketname2258 -File .\sample.txt
Enter fullscreen mode Exit fullscreen mode

List Bucket Contents

Get-S3Object -BucketName uniquebucketname2258
Enter fullscreen mode Exit fullscreen mode

Delete Bucket and its objects

Remove-S3Bucket -BucketName uniquebucketname2258 -DeleteBucketContent
Enter fullscreen mode Exit fullscreen mode

List Users

Get-IAMUserList
Enter fullscreen mode Exit fullscreen mode

Create a user

New-IAMUser -UserName Bob
Enter fullscreen mode Exit fullscreen mode

Create a new group

New-IAMGroup -GroupName DummyGroup
Enter fullscreen mode Exit fullscreen mode

Add users to group

Add-IAMUserToGroup -UserName "Bob" -GroupName "DummyGroup"
Enter fullscreen mode Exit fullscreen mode

List the group, group details and its users

$results = Get-IAMGroup -GroupName "DummyGroup"
$results
$results.Group
$results.Users
Enter fullscreen mode Exit fullscreen mode

Removes all users from a group and then deletes the group

(Get-IAMGroup -GroupName DummyGroup).Users | Remove-IAMUserFromGroup -GroupName DummyGroup -Force
Remove-IAMGroup -GroupName DummyGroup -Force
Enter fullscreen mode Exit fullscreen mode

Deletes a user

Remove-IAMUser -UserName Bob
Enter fullscreen mode Exit fullscreen mode

For more information on AWS Tools for PowerShell visit AWS Tools for PowerShell Cmdlet Reference page

Top comments (0)