<# .SYNOPSIS Checks in files in a SharePoint document library. .DESCRIPTION The script checks in a single file or loops through a document library and checks in all files. .PARAMETER url subsite url. .PARAMETER folder folder name .PARAMETER file file name .PARAMETER comment Checkin comment .PARAMETER checkinType checkin type, can be: Major, Minor or Overwrite .PARAMETER all checks out all files. .EXAMPLE PS > .\CheckIn-SPFile.ps1 -url http://nimaintra.net -folder "Shared Documents" ` >> -comment "Checked in by Administrator" -checkinType MinorCheckIn -all Checks in all files that are not already checked in to the "Shared Documents" document library as minor versions and adds a checkin comment. #> param( [string]$url, [string]$folder, [string]$file, [string]$comment, [string]$checkinType, [switch]$all ) $spAssignment = Start-SPAssignment # use a switch to get checkin type, # defaults to minor version if value # not within range. switch($checkinType) { {$_ -match "^major" } { $spCheckinType = [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn } {$_ -match "^minor" } { $spCheckinType = [Microsoft.SharePoint.SPCheckinType]::MinorCheckIn } {$_ -match "^overwrite" } { $spCheckinType = [Microsoft.SharePoint.SPCheckinType]::OverwriteCheckIn } Default { $spCheckinType = [Microsoft.SharePoint.SPCheckinType]::MinorCheckIn } } $spWeb = Get-SPWeb $url -AssignmentCollection $spAssignment # Check if All files should be checked out if($all) { # Get the folder $spFolder = $spWeb.GetFolder($folder) # Store file collection in a variable $spFileCollection = $spFolder.Files # loop through files and check out if # file is not already checked out $spFileCollection | ForEach-Object { # check if file is not checked in if($_.CheckOutType -ne "None") { Try { $_.CheckIn($comment,$spCheckinType) } Catch { "You cannot overwrite a major version file" $check = $true } Finally { if($check -eq $true) { Write-Host $_.Name Not Checked in } else { Write-Host $_.Name checked in } $check = $null } } else { Write-Host $_.Name already checked in } } } else { # store file URL in a variable $fileURL = $folder + "/" + $file $spFile = $spWeb.GetFile($fileURL) # check if file is not checked in if($spFile.CheckOutType -ne "None") { Try { $spFile.CheckIn($comment,$spCheckinType) } Catch { "You cannot overwrite a major version file" $check = $true } Finally { if($check -eq $true) { Write-Host $spFile.Name Not Checked in } else { Write-Host $spFile.Name checked in } $check = $null } } else { Write-Host $spFile.Name already checked in } } Stop-SPAssignment $spAssignment