1

I am attempting to write my first PS script and would like to check if a Name of an Office 365 group already exists in the system. So I set the vars and want to check if GN matches a group name already in the system, how can I access all the names from the Get-UnifiedGroup var?

$Groupname = "test group"
$Alias = "testing"
$AccessType = "Public"
$GN = Get-UnifiedGroup

 #Check if Group Exists already
            if ($GN = $Groupname)  
            {  
                write-Host "Group $GroupName exists Already!" -ForegroundColor Red 
            }  
            else  

New-UnifiedGroup –DisplayName "$Groupname" -Alias ="$Alias" -AccessType = "$AccessType"

enter image description here

ekad
  • 14,436
  • 26
  • 44
  • 46
Jamie_lee
  • 331
  • 4
  • 13

1 Answers1

1

You can access the name property of the variable with ."property"

if ($GN.Name -contains $Groupname) 

the -contains operator checks if an array contains your $groupname or you could do it the other way around:

if ($GroupName -in $GN.Name)

Also, for big chunks of data, you are probably better off with .Contains() array method (as it is usually faster), so like this:

if (($GN.Name).Contains($GroupName))
4c74356b41
  • 69,186
  • 6
  • 100
  • 141