# Copy sharepoint views to other libraries powershell

- Link: https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/
- Published: 2015-05-01T06:47:53+05:30
- Author: Deviprasad Panda

[CmdletBinding()]

Param
 ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$WebURL, [Parameter(
Mandatory=$true)][string]$SourceList, [Parameter(Mandatory=$true)][string]$SourceView,[
Parameter(Mandatory=$false)][string]$NewViewName, [Parameter(Mandatory=$false)][
string]$TargetURL, [Parameter(Mandatory=$false)][string]$IgnoreLibs, [Parameter(
Mandatory=$false)][string]$AsDefault, [Parameter(Mandatory=$false)][string]$OutputPath,[
Parameter(Mandatory=$false)][string]$SmtpServer, [Parameter(Mandatory=$false)][string]
$EmailFrom, [Parameter(Mandatory=$false)][string]$EmailTo )

Function Copy-SPView
 {

Write-Host &quot;Loading Sytem Modules &quot;
 Get-Module -listAvailable | import-
module

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue)-
eq $null )
 { Write-Host &quot;Loading Sharepoint Module &quot; Add-PSSnapin -Name
Microsoft.SharePoint.PowerShell

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell) -eq $null )
 { Write-Host&
quot;Failed to load sharepoint snap-in. Could not proceed further, Aborting …&quot;
Exit } }

Start-SPAssignment -Global

$SPWeb  = Get-SPWeb -Identity $WebURL -ErrorAction SilentlyContinue
 $SPWebT = Get-
SPWeb -Identity $TargetURL -ErrorAction SilentlyContinue

$ignoreList = &quot;Customized Reports&quot;,&quot;Form Templates&quot;,&quot;Shared
Documents&quot;,&quot;Site Assets&quot;,&quot;Site Pages&quot;,&quot;Style Library&
quot;,&quot;Master Page Gallery&quot;,&quot;Picture&quot; + $SourceList + $IgnoreLibs

if($SPWeb -eq $null){ Write-Host &quot;Unable to reach the provided URL, Aborting…&
quot; ;Exit }
 if( ($SPWeb.Lists.TryGetList($SourceList) ) -eq $Null){ Write-Host&
quot;The list $SourceList is not availible, Aborting …&quot;; Exit } if($AsDefault-
ne $True){$AsDefault = $False}

$SourceLists=$SPweb.lists[&quot;$SourceList&quot;]

if( ($SourceLists.Views[$SourceView]) -eq $Null ){ Write-Host &quot;The view $SourceView
does not exist, Aborting …&quot;; Exit  }
 if($NewViewName -lt 1){ $NewViewName 
= $SourceView }

# Go through each document library in the target site

$listIds = @();
 $i = 0;

if($SPWebT -ne $null)
 { $lists=$SPWebT.lists } else { $lists=$SPWeb.lists }

while ($i -lt $lists.Count)
 { $list = $lists[$i]

if($list.BaseType -eq &quot;DocumentLibrary&quot;)
 { if ($Ignorelist -contains 
$list.Title) { write-host $list &quot;is Ignored&quot; -foregroundcolor Yellow -
backgroundcolor Black } else { $view = $list.Views[$NewViewName] if ($view -ne $
null) { Write-Host &quot;Updating existing view&quot; -foregroundcolor Yellow -backgroundcolor
Black

$list.views.delete($view.ID)
 $list.update() }

$Viewfields = $Sourcelists.Views[$SourceView].ViewFields.ToStringCollection()
 $
viewRowLimit=&quot;100&quot; $viewPaged=$true $viewDefaultView=$AsDefault

# Setting the Query for the View

$viewQuery = $Sourcelists.Views[$SourceView].Query
 $viewName = $NewViewName

# Finally – Provisioning the View

try
 { $myListView = $list.Views.Add($viewName, $viewFields, $viewQuery, 100, $True,
$False, &quot;HTML&quot;, $False) } catch { Write-Host &quot;Not all columns are
availible in the target library&quot; -foregroundcolor Yellow }

# You need to Update the View for changes made to the view

# Updating the List is not enough

$myListView.DefaultView = $AsDefault
 $myListView.Update() $list.Update()

Write-Host &quot;$viewName added to Library $list&quot;
 } } $i = $i + 1 } $SPWeb.
Dispose() }

Copy-SPView ($WebURL,$SourceList,$SourceView,$NewViewName)

Using this script it’s possible to copy views from a source library to any target
library.
 This includes copying views to libraries in other site collections / web
applications or even other SharePoint servers!

**Summary of possible variables:**

– WebURL
 URL of the source library – SourceList Displayname of the source library
what contains the view – SourceView Name of the view that needs to be copied – NewViewName
Name of the view in the target libraries. (if left empty the source view name will
be used.) – TargetURL URL of the target site / site collection of web application(
If left empty the libraries in the WebURL are being updated) – IgnoreLibs Name of
the libraries that need to be ignored. (The script contains a list of SharePoint
Household Libraries that are ignored by default including the Source Library.) “
Customized Reports”,”Form Templates”,”Shared Documents”,”Site Assets”,”Site Pages”,“
Style Library”,”Master Page Gallery”,”Picture”

**.Example 1**

# This example copies the view to all document libraries within the source URL

# PS C:> .Copy-SPView.ps1 -WebURL <source URL> -SourceList <Your Source Library> -SourceView <Name of View>

**.Example 2**

# This example copies the view to all document libraries within the target URL.

# PS C:> .Copy-SPView.ps1 -WebURL <source URL> -SourceList <Your Source Library> -SourceView <Name of the View> -TargetURL “<Your target URL>”

**.Example 3**

# This example shows all possible variables that are currently working.

# PS C:> .Copy-SPView.ps1 -WebURL <source URL> -SourceList <Your Source Library> -SourceView <Name of the View> -TargetURL “<Your target URL>” -NewViewName “Rogier’s View” -IgnoreLibs “Shared Documents”

Please let me know if this was helpful

### Share this:

 * [  X ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=twitter)
 * [  Facebook ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=facebook)
 * [  WhatsApp ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=jetpack-whatsapp)
 * [  Email ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?subject=%5BShared%20Post%5D%20Copy%20sharepoint%20views%20to%20other%20libraries%20powershell&body=https%3A%2F%2Fsharepointtechnicalsupport.com%2F2015%2F05%2F01%2Fcopy-sharepoint-views-other-libraries-powershell%2F&share=email)
 * [  Telegram ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=telegram)
 * [  LinkedIn ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=linkedin)
 * [  Tumblr ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=tumblr)
 * [  Pinterest ](https://sharepointtechnicalsupport.com/2015/05/01/copy-sharepoint-views-other-libraries-powershell/?share=pinterest)

### Like this:

Like Loading…

### _Related_
