Does anyone know how to get AWS account number using AWS Powershell? Doesn't look like there's an API available for that.
Nice and simple
(Get-STSCallerIdentity).Account
...or
(Get-STSCallerIdentity -Region [your_aws_region]).Account
I was unable to comment on the other provided answer, so I'll have to offer my own solution as a slight modification.
I do believe the OwnerId on all groups will be the Account Id. However you may not have a "default" group. I recommend leaving out the -GroupNames "default". Also, I'm showing my example using a SAML token, as that is our case coming in with AD authorization.
$awsAccountNumber = (get-ec2securitygroup -ProfileName saml -Region us-west-2)[0].OwnerId
Hopefully that will be of some use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With