Blog Image

PowerShell

SharePoint Powershell Commands

GoSharePoint Website
Documentation Blog

This blog is based on SharePoint 2013 PowerShell Commands. Please visit my other blog which contains SharePoint Documentation.

Remove Ampisands from files

Administration Posted on Sat, September 05, 2020 21:43:07

$path = ‘c:\datasource’

$items = Get-ChildItem -Path $path -Filter ‘&.*’ -Recurse

foreach ($item in $items)
{
try
{
$item.Name
Rename-Item -Path $item.FullName -NewName $item.Name.Replace(‘&’,’_’)

    $output = "Amended '" +  $item.Name + "' to '" + $item.Name.Replace('&','_') + "'"
    Write-Host $output
}
catch
{
    $output = "Could not rename file '" +  $item.Name + "'"
    Write-Host $output
}

}

$items = Get-ChildItem -Path $path -Filter ‘~.‘ -Recurse

foreach ($item in $items)
{
try
{
$item.Name
Rename-Item -Path $item.FullName -NewName $item.Name.Replace(‘~’,’_’)

    $output = "Amended '" +  $item.Name + "' to '" + $item.Name.Replace('~','_') + "'"
    Write-Host $output
}
catch
{
    $output = "Could not rename file '" +  $item.Name + "'"
    Write-Host $output
}

}



Set External Sharing For Sites

Administration Posted on Sat, September 05, 2020 21:40:23

function SetExternalSharingForSites($csvrows, $adminUrl, $credentials, $logFile)
{
#Connect to Admin site and set sharing properties for all sites in CSV List
Connect-SPOService -Url $adminUrl -Credential $credentials;
foreach($row in $csvrows)
{
if ($row.RequestType -eq ‘Adhoc’ -or $row.RequestType -eq ‘Extranet’)
{

        try
        {
            if ($sitesWithErrors.Contains($row.Url) -eq $false)
            {
                WriteToLog -logFile $logFile -text ("Attempting to set External Sharing for " + $row.url) -ForegroundColor Yellow
                Set-SPOSite -Identity $row.url -SharingCapability ExistingExternalUserSharingOnly
                Set-SPOSite -Identity $row.Url -DisableSharingForNonOwners
                WriteToLog -logFile $logFile -text ("External Sharing enabled for " + $row.url) -ForegroundColor Green
            }
        }
        catch
        {
            WriteToLog -logFile $logFile -text ("Could not enable External Sharing for " + $row.url + '. ' + $Error[0].Exception.GetBaseException()) -ForegroundColor Red
            $sitesWithErrors.Add($row.Url,"Could not enable External Sharing");
        }
    }
}
Disconnect-SPOService

}



Connect To Tenant

Administration Posted on Sat, September 05, 2020 21:37:51

function ConnectToTenant($credentials)
{
# Connect to to Office 365
try{
Connect-SPOService -Url $global:adminUrl -Credential $credentials -ErrorAction Stop
write-host “Info: Connected succesfully to Office 365” -foregroundcolor green;
return $true;
}
catch{
write-host “Error: Could not connect to Office 365” -foregroundcolor red;
break;
}
}



Grant Site Collection Admin

Administration Posted on Sat, September 05, 2020 21:16:44

$adminUrl = ‘https://tenant-admin.sharepoint.com’;

if ($credentials -eq $null)
{
$credentials = Get-Credential
}

Connect-SPOService -Url $adminUrl -Credential $credentials -ErrorAction Stop

$siteUrl = ‘https:// tenant .sharepoint.com/sites/marketing

Set-SPOUser -Site $siteUrl -LoginName ‘username@domain.com’ -IsSiteCollectionAdmin $true