Asadullah Awan
Asadullah Awan

Reputation: 25

get All resources on azure powershell whose Tags start with (or include) a particular String - Azure Powershell

I have almost 20 resources in azure, 4 of them have been given Tags @

{"Office1work"="work"}
{"Office2practice"="Practice"}
{"Office3practice"="Practice"}
{"Office4practice"="Practice"}

Now I want to get the resources whose Tag names start with the keyword "Office". I know to get a resource by a TagName,for example "hello", I simply use the following command,

get-azureRmResource -TagName "Hello"

How can I use the -Tagname property of get-azurermresource to give me all resources whose tags are starting with the keyword "Office" ?

Or is there any other good method to get all resources whose Tags start with a particular string?

Thanks :)

Upvotes: 1

Views: 5327

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

You can use this code snippet:

$resources = Get-AzureRmResources
$resources.foreach{ if ($PSItem.tags.keys -match '^Office') { $PSItem } }

First you get all the resources in the subscription, then you filter out all the resource whose tags do not match the 'Office' "expression".

as @LotPings points out, it would probably make more sense to filter without saving to a temporary variable:

$resources = Get-AzureRmResources|Where-Object {$_.tags.keys -match "^Office"}

Also, I didnt notice you were asking for a starts with filter, so you should use ^Office as a more strict filter (if you need to).

Upvotes: 3

Related Questions