Search code examples
powershellms-officeazure-powershellms-office-script

Powershell get spare licenses


I am trying to get the amount of spare licenses available from the Get-MsolAccountSku command.

Here's the code (output below)

$Licenses = Get-MsolAccountSku
$spare = Foreach ($License in $licenses)
{
  ($License.ActiveUnits - $License.ConsumedUnits)   
}

Get-MsolAccountSku | Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,@{L=’SpareLicenses’;E={$spare}}

I want to add a column to the right of the output to list the amount of licenses available from the subtraction in the ForEach loop.

ActiveUnits ConsumedUnits
----------- -------------
         30            26
       1601             1
         30            29
         25             0
          5             3
          1             0
      12550         12465
    1000000         12461
      12550         12466
      12555         12468
         31            19
      12550         12464

Solution

  • Your $spare object is not needed, just update the Select to do the calculation...

    Get-MsolAccountSku | 
    Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,
    @{L=’SpareLicenses’;E={$_.ActiveUnits - $_.ConsumedUnits}}