33

I'm trying to verify if ResourceGroup exist or not so i thought that following code should return true or false, but it doesn't output anything.

$RSGtest = Find-AzureRmResource | Format-List ResourceGroupName | get-unique
$RSGtest -Match "$myResourceGroupName"

Why am I not getting any output?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Peter Pavelka
  • 511
  • 1
  • 5
  • 16

5 Answers5

54

Update:

You should use the Get-AzResourceGroup cmdlet from the new cross-plattform AZ PowerShell Module now. :

Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}

Original Answer:

There is a Get-AzureRmResourceGroup cmdlet:

Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • 1
    Thanks, but there is something wrong, here is all i getting out of it: WARNING: The output object type of this cmdlet will be modified in a future release. – Peter Pavelka Jun 02 '16 at 17:52
  • and $notPresent is not set when there is no resource group? If so, assign the return value to an variable and check that... – Martin Brandl Jun 02 '16 at 17:57
  • 3
    For those struggling with `-ev` and `-ea` - those are shorthands for `-ErrorVariable` and `-ErrorAction`. I know it tripped me, so here is a [reference](https://blogs.msdn.microsoft.com/powershell/2006/11/02/erroraction-and-errorvariable/) – trailmax Feb 27 '18 at 11:41
  • 2
    @trailmax You are right, I shouldn't use aliases in my snippets. Will change it – Martin Brandl Feb 27 '18 at 11:45
  • @MartinBrandl Well, yes and no. If not for this answer with shorthands I'd never investigate those common parameters (seen them before) and turned out that I've been powershelling wrong for a while -) – trailmax Feb 27 '18 at 12:07
  • :-). However, I usually only use them when using the interactive shell. Thanks for your comment. – Martin Brandl Feb 27 '18 at 13:18
  • 1
    Thank you for updating to add the new cmdlet update! – Sam Apr 18 '20 at 13:28
  • Unless of course the `Get-AzResourceGroup` error was due to some other completely unrelated error (e.g. network issue) and the RG does exist. For this reason I prefer the other answer where you enumerate all the RGs and search for the desired one (sure, it's less performant, but in most cases you aren't going to have that many RGs in a single sub) – Ohad Schneider Mar 16 '22 at 20:48
5

try this

$ResourceGroupName = Read-Host "Resource group name"
Find-AzureRmResourceGroup | where {$_.name -EQ $ResourceGroupName}
Pawan Dubey
  • 192
  • 3
  • 15
  • In the new Az Powershell version: `$existingResourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroup } | Select-Object -First 1; if (!$existingResourceGroup) { ... }` – Ohad Schneider Mar 16 '22 at 20:49
3

I am a PS newbie and I was looking for a solution to this question.

Instead of searching directly on SO I tried to investigate on my own using PS help (to get more experience on PS) and I came up with a working solution. Then I searched SO to see how I compared to experts answers. I guess my solution is less elegant but more compact. I report it here so others can give their opinions:

if (!(Get-AzResourceGroup $rgname -ErrorAction SilentlyContinue))
   { "not found"}
else
   {"found"}

Explanation of my logic: I analyzed the Get-AzResourceGroup output and saw it's either an array with found Resource groups elements or null if no group is found. I chose the not (!) form which is a bit longer but allows to skip the else condition. Most frequently we just need to create the resource group if it doesn't exist and do nothing if it exists already.

Anton M
  • 798
  • 7
  • 17
1

I was also looking for the same thing but there was a additional condition in my scenario.

So I figured it out like this. To get the scenario details follow

$rg="myrg"
$Subscriptions = Get-AzSubscription
$Rglist=@()
foreach ($Subscription in $Subscriptions){
$Rglist +=(Get-AzResourceGroup).ResourceGroupName
}
$rgfinal=$rg
$i=1
while($rgfinal -in $Rglist){
$rgfinal=$rg +"0" + $i++
}
Write-Output $rgfinal
Set-AzContext -Subscription "Subscription Name"
$createrg= New-AzResourceGroup -Name $rgfinal -Location "location"
0

Had a similar challenge, I solved it using the script below:

$blobs = Get-AzureStorageBlob -Container "dummycontainer" -Context $blobContext -ErrorAction SilentlyContinue

## Loop through all the blobs
foreach ($blob in $blobs) {
    write-host -Foregroundcolor Yellow $blob.Name
    if ($blob.Name -ne "dummyblobname" ) {
        Write-Host "Blob Not Found"
    }
    else {
        Write-Host "bLOB already exist"
    }
}