So, I've found my perfect PowerShell script to archive Groups in a really nice way. The PS script is interactive and "asks some questions" (Prompt command), so to speak. :) The script looks as follows:

$CheckGroup = Read-Host -Prompt "Enter alias of group to archive"

$AGroup = (Get-UnifiedGroup $CheckGroup -ErrorAction SilentlyContinue)

If ($AGroup) {

Write-Host "Archiving" $AGroup.DisplayName -ForegroundColor Yellow

} Else {

Write-Host $CheckGroup "group not found - terminating"

Return }

# Get lists of current owners and members

$CurrentOwners = (Get-UnifiedGroupLinks -Identity $AGroup.Alias
-LinkType Owners | Select Name)

$CurrentMembers = (Get-UnifiedGroupLinks -Identity $AGroup.Alias
-LinkType Members | Select Name)

# Add a new owner - this is the address of the account that will continue to access the group

$AdminAccount = "Compliance Administrator"

Add-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Members -Links $AdminAccount

Add-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Owners -Links $AdminAccount

# Remove the other members and owners

ForEach ($O in $CurrentOwners) {

Remove-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Owners
-Links $O.Name


ForEach ($M in $CurrentMembers) {

Remove-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Members
-Links $M.Name


# Create SMTP Address for the archived group

$OldSmtpAddress = $AGroup.PrimarySmtpAddress -Split "@"

$NewSmtpAddress = $OldSmtpAddress[0] + "_archived" + "@" + $OldSmtpAddress[1]

$AddressRemove = "smtp:"+$AGroup.PrimarySmtpAddress

# Update Group properties

Set-UnifiedGroup -Identity $AGroup.Alias -AccessType Private
-RequireSenderAuthenticationEnabled $True -HiddenFromAddressListsEnabled $True -CustomAttribute1 "Archived" -CustomAttribute2 (Get-Date -Format s) -PrimarySmtpAddress $NewSmtpAddress

Set-UnifiedGroup -Identity $AGroup.Alias -EmailAddresses @{remove=$AddressRemove}

Write-Host $AGroup.DisplayName "is now archived and" $AdminAccount "is the new group owner

My question:

I wonder, If there's a possibility to make this script "interactive" on an ordinary Sharepoint site? So the assistant can go to this site, execute the PS script and run it completely on the site?


There is a way. Although it's theoretical.

  1. Modify your PowerShell script so that it also hosts a mini-web server.

You can use two PowerShell modules for that:

Import-Module Polaris -Verbose
Import-Module PSHTML -Verbose
Add-Type -AssemblyName System.Web

http://chen.about-powershell.com/2018/09/exploring-pshtml-for-polaris-yet-another-way-to-build-forms-using-powershell/ https://chen.about-powershell.com/2018/09/building-a-simple-form-using-powershell-polaris-module/

Example. Credits to Chendrayan Venkatesan:

Import-Module Polaris -Verbose
Import-Module PSHTML -Verbose
Add-Type -AssemblyName System.Web

New-PolarisGetRoute -Path "/Login" -Scriptblock {
    $HTML = html {
        head {
            Title "Login Page"

        body {
            h1 -Content "Connect to Office365 Securely..."
            Form -action "/authenticate" -method 'post' -target '_blank' -Content {
                input -type text -name 'uSite'
                input -type text -name 'uName' 
                input -type password -name 'uPassword'
                button -Content "Click"

New-PolarisPostRoute -Path "/authenticate" -Scriptblock {
    $Body = [System.Web.HttpUtility]::UrlDecode($Request.BodyString)
    $Data = @{}
    $Body.split('&') | ForEach-Object {
        $part = $_.split('=')
        $Data.add($part[0] , $part[1])
    Import-Module C:\projects\iPolaris\assemblies\Microsoft.SharePoint.Client.dll
    $UserName = $Data.uName 
    $Pasword = $Data.uPassword | ConvertTo-SecureString -AsPlainText -Force 
    $ClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Data.uSite)
    $ClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($UserName, $Pasword)
    $ListCollection = $ClientContext.Web.Lists
    $Collection = @()
    foreach ($List in $ListCollection | Select-Object -First 10) {
        $Result = [pscustomobject]@{
            Title     = $List.Title 
            ItemCount = $List.ItemCount
        $Collection += $Result
    $Response.Send(($Collection | ConvertTo-Json))

Start-Polaris -Port 8080
  1. Make sure that your mini-webserver checks the URLs. For example, you can pass parameters to your PowerShell script with URL: http://hostName:8080/GroupToArchive

  2. In SharePoint, you can add some simple HTML Link

    <a href='http://hostName:8080/GroupToArchive'>Click to archive this SharePoint site</a>

As you can see, this is just a general idea, but you can steal it to implement it the way it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.