Reputation: 331
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"
Upvotes: 1
Views: 1067
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