Q:

Can I check if global arrayList exist within a function and create+add to it if it doesn't? 

I want to add an item to a global array list that does not necessarily exists beforehand.  Here is what I'm trying:
function runGrunt ($fwd="./projectFolder", $argList=@())
{
$cmdProc=start-process powershell -ArgumentList "-noexit",("-command grunt "+ [string]$argList) -WorkingDirectory $fwd -PassThru
[System. Collections. ArrayList]$Global:shells. Add(($cmdProc))
}

if $Global:shells arrayList is defined globally before function call - it works, if it's undefined I get an error 
You cannot call a method on a null-valued expression. 
At line:14 char:1
+ [System. Collections. ArrayList]$Global:shells. Add(($cmdProc))

Can I somehow make it create the $Global:shells arrayList if it doesn't yet exist, and just add item to it, if it does?  It seems to try to cast $null as an arrayList and obviously fails, can I work-around it somehow?  I can use Get-Variable shells -Scope global but I get an object, not a boolean value and I'm kinda lost on how to convert it since it doesn't have an isEmpty() method or anything else suitable. 

A:

Just check if your global variable object is null and create it if needed.  Also don't forget to check that it is of the correct type every time you call type specific methods in PowerShell as a good practice. 
function runGrunt ($fwd="./projectFolder", $argList=@())
{
   $cmdProc=start-process powershell -ArgumentList "-noexit",("-command grunt "+ [string]$argList) -WorkingDirectory $fwd -PassThru

   if($null -eq $global:shells) # note that $global:shells -eq $null would not work due to the way how comparison operator work in PowerShell
   {
      $global:shells = New-Object System. Collections. ArrayList
   }
   elseif($global:shells. GetType() -eq [System. Collections. ArrayList]
   {
      $global:shells. Add(($cmdProc))
   }
   else
   {
      Write-Error "Global variable 'shells' is not of expected type System. Collections. ArrayList.  Type is: $($global:shells. GetType())"
   }
}

