Skip to content

Remove-User-SPO

SharePoint Online: Removes a user from a site collection

#Requires -Version 5.1
#Requires -Modules Microsoft.Online.SharePoint.PowerShell

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$LoginName,
    [Parameter(Mandatory = $true)]
    [string]$Site,
    [string]$Group
)

Process {
    try {
        $cmdArgs = @{ ErrorAction = 'Stop'; Site = $Site; LoginName = $LoginName }
        if ($PSBoundParameters.ContainsKey('Group')) { $cmdArgs.Add('Group', $Group) }
        $result = Remove-SPOUser @cmdArgs | Select-Object *
        if ($null -ne $result) { foreach ($item in $result) { $item | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force } }
    }
    catch { throw }
}

User name to remove

URL of the site collection

Group to remove the user from (optional)

An interactive directory of PowerShell scripts.