Jamie_lee
Jamie_lee

Reputation: 331

Get name of all Office 365 groups

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

Upvotes: 1

Views: 1067

Answers (1)

4c74356b41
4c74356b41

Reputation: 72176

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))

Upvotes: 1

Related Questions