Monday, January 22, 2018

Useful Powershell commands for SP Online

For any commands to run below first always use first command which is connect to tenant. Once session is established you can run individual commands.

$secPassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("username@xxx.com", $secPassword)
Connect-PnPOnline –Url https://xxx.sharepoint.com-admin.sharepoint.com –Credentials $cred

Connecting to Tenant:
Connect-SPOService -url https://imflab-admin.sharepoint.com -Credential $credential
Connect-PnPOnline -Url https://tenant.microsoft.com

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass
#Connect to Microsoft Teams Connect-MicrosoftTeams -Credential $credential New-Team -DisplayName "Team5" -Description "Team to test classification" -Visibility "Private" -Classification "LBI"

https://morgantechspace.com/2021/09/connect-to-sharepoint-site-with-mfa-account-using-csom-and-powershell.html - Connect SPO using MFA andf CSOM

Create a new site collection similar to creation from SP Admin portal:
New-SPOSite -Url https://imfo365.sharepoint.com/sites/dev -Owner nvaddlamudi@imfo365.onmicrosoft.com -StorageQuota 1024 -Title "raoteamsite1" -CompatibilityLevel 15 -LocaleID 1033 -ResourceQuota 300 -Template "STS#0" -NoWait

Set-SPOOrgNewsSite -OrgNewsSiteUrl "https://imfdev.sharepoint.com/sites/Coronavirus/" - Set org news site
Get-SPOOrgNewsSite 

Powerapps and Powerautomate disable and enable
Set-SPOSite –Identity https://imfdev.sharepoint.com/sites/Comm2 -DisableFlows Disabled
Set-SPOSite –Identity https://imfdev.sharepoint.com/sites/Comm2 -DisableAppViews Disabled
Set-SPOSite –Identity https://imfdev.sharepoint.com/sites/Comm2 -DisableFlows NotDisabled
Set-SPOSite –Identity https://imfdev.sharepoint.com/sites/Comm2 -DisableAppViews NotDisabled

Site Classification using PnP
Connect-PnPOnline -Scopes "Directory.ReadWrite.All"
Enable-PnPSiteClassification -Classifications "HBI","LBI","Top Secret" -UsageGuidelinesUrl "http://aka.ms/sppnp" -DefaultClassification "HBI"

Add SPO Site collection App catalog
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Connect-SPOService -url https://imfo365lab-admin.sharepoint.com -Credential $credential
$site = Get-SPOSite https://imfo365lab.sharepoint.com/sites/DM
Add-SPOSiteCollectionAppCatalog -Site $site

Create a new communication site collection similar to user creating site from SharePoint.aspx home page. This uses PNP powershell:
Connect-PnPOnline –Url https://imfo365-admin.sharepoint.com –Credentials (Get-Credential)
New-PnPSite -Type CommunicationSite -Title raocommsite1 -Url https://imfo365.sharepoint.com/sites/raocommsite1 -SiteDesign Showcase -AllowFileSharingForGuestUsers

New-SPOSite -Url https://imfdev.sharepoint.com/sites/c4 -Owner nvaddlamudi@imfdev.onmicrosoft.com -StorageQuota 1000 -CompatibilityLevel 15 -LocaleID 1033 -Template "SITEPAGEPUBLISHING#0" -TimeZoneId 13 -Title "c4 site collection"

Modern Team site without group
New-SPOSite -Url https://imfo365lab.sharepoint.com/sites/DM -Owner nvaddlamudi@lab.imf.org -StorageQuota 1000 -CompatibilityLevel 15 -LocaleID 1033 -Template "STS#3" -TimeZoneId 13 -Title "DM1"

Deleting a site collection:
Remove-SPOSite -Identity "https://imfo365.sharepoint.com/sites/dev" -Confirm:$false -NoWait

Deleting a site collection permanently from recyclebin:
Remove-SPODeletedSite -Identity "https://imfo365.sharepoint.com/sites/dev" -Confirm:$false

View Custom Scripts setting for site collection:
$site=Get-SPOsite https://imfo365.sharepoint.com/sites/modernteam

$site.DenyAddAndCustomizePages

Update Custom Script settings of a site collection:
Set-SPOsite https://imfo365.sharepoint.com/sites/Dev -DenyAddAndCustomizePages 1 - No more custom scripts (Enabled)

Set-SPOsite https://imfo365.sharepoint.com/sites/Dev -DenyAddAndCustomizePages 0 - Custom scripts are allowed(Disabled)

Set-SPOsite https://imfo365.sharepoint.com/sites/Dev -DenyAddAndCustomizePages $false - Custom scripts are allowed(Disabled)

List all site collections in a tenant:
$sites = Get-SPOSite
#Retrieve and print all sites
foreach ($site in $sites)
{
    Write-Host 'Site collection:' $site.Url 
}

List all site collections and corresponding properties in a tenant:
$sitecollections = Get-SPOSite -Limit ALL  | Export-Csv -Path "D:\temp\SPO.csv" -Delimiter ';' -NoTypeInformation;

Listing properties of a SPO Site:
$site=Get-SPOsite "https://imfo365.sharepoint.com/sites/Dev"
$site.Url
$site.LastContentModifiedDate
$site.Status
$site.ResourceUsageCurrent
$site.ResourceUsageAverage
$site.StorageUsageCurrent
$site.LockIssue
$site.WebsCount
$site.CompatibilityLevel
$site.Url
$site.LocaleId
$site.LockState
$site.Owner
$site.StorageQuota
$site.StorageQuotaWarningLevel
$site.ResourceQuota
$site.ResourceQuotaWarningLevel
$site.Template
$site.Title
$site.AllowSelfServiceUpgrade
$site.DenyAddAndCustomizePages
$site.SharingCapability

For more properties check below article
https://www.risual.com/2015/07/31/sharepoint-online-management-shell-part-4-enumerating-site-collections/

Monitor Site collection storage quota:
function Collect-SiteCollectionInfo
{
    Param(
    [Parameter(Mandatory=$true)]
    [ValidateNotNull()]
    [string]$outputFullFilePath,
    [Parameter(Mandatory=$false)]
    [switch]$selectSites,
    [Paremeter(Mandatory=$false)]
    [char]$csvSeparator = ';'
    )
    Write-Host "Collecting site collections";
    $sitecollections = $null;
    if($selectSites)
    {
        $sitecollections = Get-SPOSite -Limit ALL -Detailed | Out-GridView -Title "Select site collections from which to collect data." -PassThru;
    }
    else
    {
        $sitecollections = Get-SPOSite -Limit ALL -Detailed;
    }
    $itemArray = @();
    foreach($item in $sitecollections)
    {
        [double]$storageUsed = $item.StorageUsageCurrent;
        [double]$storageQuota = $item.StorageQuota;
        [double]$ratio = ($storageUsed / $storageQuota) * 100;
        [string]$percentage = $ratio.ToString("N1") + "%";
        $temp = New-Object System.Object;
        $temp | Add-Member -MemberType NoteProperty -Name "Title" -Value $item.Title;
        $temp | Add-Member -MemberType NoteProperty -Name "Url" -Value $item.Url;
        $temp | Add-Member -MemberType NoteProperty -Name "WebsCount" -Value $item.WebsCount;
        $temp | Add-Member -MemberType NoteProperty -Name "LastContentModifiedDate" -Value $item.LastContentModifiedDate;
        $temp | Add-Member -MemberType NoteProperty -Name "StorageUsageCurrent" -Value $item.StorageUsageCurrent;
        $temp | Add-Member -MemberType NoteProperty -Name "StorageQuota" -Value $item.StorageQuota;
        $temp | Add-Member -MemberType NoteProperty -Name "StoragePercentageUsed" -Value $percentage;
        $itemArray += $temp;
    }
    $itemArray | Export-Csv -Path $outputFullFilePath -Delimiter $csvSeparator -NoTypeInformation;
    Write-Host "Succesfully collected sitecollection information.`r`nFile saved at $outputFullFilePath" -ForegroundColor Green;
}

Run Set-SPOHomeSite - HomeSiteUrl  - Setting site as SPO home site

No comments: