Terminate EC2 Spot/On-Demand instance by name

Declare function to terminate instance by Id function ec2-terminate-instance($instid){ $ec2.TerminateInstances((new-object Amazon.EC2.Model.TerminateInstancesRequest).WithInstanceId($instid)).TerminateInstancesResult.TerminatingInstance[0]} Get instance by name and terminate it $filter = (new-object Amazon.EC2.Model.Filter).WithName(‘tag:Name’).WithValue(‘YourInstanceName’) (Get-EC2Instance -Filter $filter