Tag Archives: PowerShell

SharePoint 2007

Get Inventory of All SharePoint Documents Using Windows PowerShell

I thought this was a great find when looking to assess site usage before planning data migration to SharePoint 2013. I haven’t run it across the entire farm yet due to the time it will take to run; however, I’ve tested it and posted the code below the link to show some changes I’ve made to get it to work in my environment. I encourage you to read the comments when following the link for some troubleshooting if you run into trouble.

Getting an Inventory of All SharePoint Documents Using Windows PowerShell « SharePoint Automation.

[code language=”powershell”]
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)

function Get-DocInventory([string]$siteUrl) {
$site = New-Object Microsoft.SharePoint.SPSite $siteUrl
foreach ($web in $site.AllWebs) {
foreach ($list in $web.Lists) {
if ($list.BaseType -ne “DocumentLibrary”) {
continue
}

foreach ($item in $list.Items) {
$data = @{
“Site” = $site.Url
“Web” = $web.Url
“list” = $list.Title
“Item ID” = $item.ID
“Item URL” = $item.Url
“Item Title” = $item.Title
“Item Created” = $item[“Created”]
“Item Modified” = $item[“Modified”]
“File Size” = $item.File.Length/1KB
}
New-Object PSObject -Property $data
}
}
$web.Dispose();
}
$site.Dispose()
}

Get-DocInventory | Out-GridView
Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\Temp\Report_SC_Docs.csv
[/code]

SharePoint 2007

Remove old versions from a SharePoint 2007 list

This is a nice PowerShell script to remove old versions from a SharePoint 2007 list or document library. If versioning had been turned on a list with no limits and then limits are applied, this is an easy way to go back and get the list in line with current versioning settings. Change “http://urlofsite”, $versionstokeep and “Title of List” to fit your environment.

[code language=”powershell”]
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)
$site = New-Object Microsoft.SharePoint.SPSite(“http://urlofsite”)
$SPweb = $site.OpenWeb()
$versionsToKeep = 10;
$SPlist = $SPweb.Lists[“Title of List”]
“Web is : ” + $SPweb.Title
“List is :” + $SPList.Title + ” with item count ” + $SPList.ItemCount
foreach ($SPitem in $SPlist.Items)
{
$currentVersionsCount= $SPItem.Versions.count
“current version count is :” + $currentVersionsCount
“versions to keep is :” + $versionsToKeep
if($currentVersionsCount -gt $versionstoKeep)
{
for($i=$currentVersionsCount-1; $i -gt $versionstoKeep; $i–)
{
$SPItem.versions[$i].delete()
“removed versions”
}
}
}
[/code]

SharePoint 2007

Setting Permissions to Run PowerShell Scripts | cyprich.com

When PowerShell is installed, it sets the default execution policy to Restricted which allows commands to be executed, not scripts. I don’t think this is the best approach for the application since the scripting is the main reason why people would install and use PowerShell.

You can determine what the execution policy is with this command:

Get-ExecutionPolicy

There are 4 options for execution policy: Restricted, AllSigned, RemoteSigned, and Unrestricted. The last 3 options allow scripts to be run in the shell. I prefer using the RemoteSigned policy over AllSigned because it doesn’t require a digital signature from a trusted publisher to run scripts.

You can set the execution policy for running scripts with the following command:

Set-ExecutionPolicy RemoteSigned

via Setting Permissions to Run PowerShell Scripts | cyprich.com.