Quick take: Using PowerShell split method for Azure scripts

When running scripts in Microsoft Azure, the PowerShell split method can be a handy tool in your arsenal. A good example is when getting resourceID of any given object. Let’s use for example the output of Get-AZResource for any given VM in my subscription. We can see that resourceID contains all the information to find the resource including subscription, resource group, type of resource, and the resource name itself.

split method

Using PowerShell, we can store all the output of the previous cmdlet in a variable ($vTemp), and after that, we can list only the content of the resourceID field. Here is an example:


split method

Here comes the split method to the rescue. We are going to split that entire string with the character “/” using the following cmdlet.

$vText = $vTemp.ResourceID.Split("/")

Now the $vtext variable contains one entry for each object. Here is an example of the output by checking the $vText, and also a specific position ($vtext[1] for example).

Write-Host "Original content................: " + $vTemp.ResourceId
Write-Host "Content of variable vtext.......: "
Write-Host "Content of the first position...: " + $vText[1]
Write-Host "Enumerating all values within variable vtext.."
Last but not least we can list all the content, one by one using a Do loop.
} While ($i++ -le ($vText).Count)

Featured image: Pixabay

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top