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



Configure People Picker

Administration Posted on Tue, June 28, 2016 07:03:20

Set-ExecutionPolicy -ExecutionPolicy “Unrestricted” -Force

Write-Host “”

Write-Host “========================================================”

Write-Host “People Picker – Adding Trusts……”

Write-Host “- Set an encryption key for use with a one-way trust ”

Write-Host “- Enable cross-forest or cross-domain queries when you use a one-way trust”

Write-Host “- Set an encryption key for use with a one-way trust ”

Write-Host “- Set an encryption key for use with a one-way trust ”

Write-Host “https://technet.microsoft.com/en-gb/library/gg602075.aspx”

Write-Host “========================================================”

Write-Host “”

$URLList = @()

$URLList += “http://spsite”

$URLList += “http://spsite:<Central Admin Port>”

#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

try

{

Write-Host “- Updating encryption key”

stsadm -o setapppassword -password <passphase>

foreach ($URL in $URLList)

{

Write-Host “- Enable cross-forest or cross-domain queries for” $URL

STSADM -o setproperty -pn peoplepicker-searchadforests -pv “domain:<ad.domain.com,AD\xxx_SP_Profile,<service account password>;forest:ad.forestdomain.com,AD\xxx_SP_Profile,<service account password>” -url $URL

}

}

catch { Write-Output $_ }



Create Alternate Access Mappings

Administration Posted on Tue, June 28, 2016 06:57:25

#Examples of Alternate Access Mappings
#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

#create the public URL

New-SPAlternateURL -URL “http://spsite” -Zone “Default” -WebApplication “SharePoint – 80” -ErrorAction SilentlyContinue

New-SPAlternateURL -URL “https://spsite” -Zone “Intranet” -WebApplication “SharePoint – 80” -ErrorAction SilentlyContinue

New-SPAlternateURL -URL “https://bi.spsite.com” -Zone “Internet” -WebApplication “SharePoint – 80” -ErrorAction SilentlyContinue

New-SPAlternateURL -URL “https://spsite” -Zone “Default” -WebApplication “SharePoint – 80” -ErrorAction SilentlyContinue

New-SPAlternateURL -URL “http://spsite.com” -Zone “Intranet” -WebApplication “SharePoint – 80” -ErrorAction SilentlyContinue



Set the super user account utilized by the cache

Administration Posted on Tue, June 28, 2016 05:05:02

#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

$w = Get-SPWebApplication “http://sprootsite/”

$w.Properties[“portalsuperuseraccount”] = “i:0#.w|AD\xxx_SP_Farm”

$w.Properties[“portalsuperreaderaccount”] = “i:0#.w|AD\xxx_SP_Farm”

$w.Update()



Get Expired Users from List

Administration Posted on Tue, June 28, 2016 01:44:17

Import-Module ActiveDirectory

function FileTime2Date($time) {

return ([datetime]::FromFileTime($time)).DateTime

}

$today = Get-Date

$userlist = Get-Content “C:\temp\GetExpiredUsersfromList.txt”

Get-ADUser -Filter * -Properties * |

select sAMAccountName, accountExpirationDate, @{n=’passwordExpiry’;

e={FileTime2Date $_.’msDS-UserPasswordExpiryTimeComputed’}} |

? { $userlist -contains $_.sAMAccountName -and (

$_.accountExpirationDate -le $today -or

$_.passwordExpiry -le $today

)

}



Next »