Tempuslight
Tempuslight

Reputation: 1223

Trying to run a script, using Exchange Online, connection works, Get-UnifiedGroup works, but Set doesn't exist?

So I'm trying to run a script that hides certain email addresses from a client's outlook addressbook.

$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl | Set-UnifiedGroup -HiddenFromAddressListsEnabled $true 
Remove-PSSession $Session

This will hide all the email addresses that have a sharepoint URL connected to them from creating team sites. Anyway, Get-UnifiedGroup works, but when I do the following:

Set-UnifiedGroup -HiddenFromAddressListsEnabled $true  

it says this:

Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.  

I've searched google but can't seem to find a proper solution to my problem... I also did the following commands, to no avail:

Set-ExecutionPolicy RemoteSigned  

and

Install-Module MSOnline  

I don't know where to look anymore, I'm by not means used to writing Powershell...

Upvotes: 0

Views: 4594

Answers (1)

CraftyB
CraftyB

Reputation: 746

Looking at the Microsoft documentation "Set-UnifiedGroup -HiddenFromAddressListsEnabled" does not take pipeline input.

(https://learn.microsoft.com/en-us/powershell/module/exchange/users-and-groups/set-unifiedgroup?view=exchange-ps)

I would suggest trying:

$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$groups = Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl

foreach ($group in $groups){
    set-unifiedgroup -identity $group.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true
}

Remove-PSSession $Session

With regards to "Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet"

You will need to add the appropriate role in the exchange admin centre to be able to access the cmdlet.

You will need to be an administrator to make these changes;

https://outlook.office365.com/ecp/?rfr=Admin_o365

Login and select "Permissions" on the left.

I think "Organization Management" should give you the cmdlet you are looking for.

Edit that role and add the user that you are using for the above powershell script to the members list.

This will usually take 30-60 minutes to become effective.

Upvotes: 1

Related Questions