I have SPO site and trying to check in checked out files (also inside folders) under "Shared Document" library. This PS script should checked in all files under single document library. I have tried below power shell code but it throws error.

    Connect-SPOService –Url https://***-admin.sharepoint.com –Credential [email protected]
Add-Type -Path "E:/sp/Microsoft.SharePoint.Client.dll"   
Add-Type -Path "E:/sp/Microsoft.SharePoint.Client.Runtime.dll"   

$spWeb = Get-SPWeb "https://***.sharepoint.com/sites/tesst"
$listName = "Shared Documents"
$list = $spWeb.Lists |? {$_.Title -eq $listName}
foreach ($item in $list.Items) 
    $itemFile = $item.File
    if( $itemFile.CheckOutStatus -ne "None" )
        $itemFile.CheckIn("Automatic CheckIn. (Administrator)")


Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:10
 $spWeb = Get-SPWeb "https://coctees.sharepoint.com/sites/tesst"
     CategoryInfo          : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
     FullyQualifiedErrorId : CommandNotFoundException


  • could you please add the detail about error, it will help to understand the issue
    – Waqas Sarwar MVP
    Apr 3, 2018 at 14:17

2 Answers 2


I did some job using PowerShell + REST API. My Script bellow is to create a site inside a site collection. I have taken it from a website. This has shown me how to deal with SPO using powershell and REST API


$TenantUrl = "https://yoursitel.sharepoint.com" $Username = "sharepoint@yoursite" $Password = 'password'

$SiteTitle = $args[0] $SiteUrl = "https://yoursitel.sharepoint.com""+ $args[1] # Note this URL must be available (check with "/_api/GroupSiteManager/GetValidSiteUrlFromAlias") $SiteTemplate = "" # "Topic" => leave empty (default), "Showcase" => "6142d2a0-63a5-4ba0-aede-d9fefca2c767" and "Blank" => "f6cc5403-0d63-442e-96c0-285923709ffc"

Communication site creation request

$RequestBody = '{"request":{"__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},"Title":"' + $SiteTitle + '","Url":"' + $SiteUrl + '","Description":"","Classification":"","SiteDesignId":"' + $SiteTemplate + '","AllowFileSharingForGuestUsers":false}}'

if ($SiteTemplate -eq "") { $RequestBody = '{"request":{"__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},"Title":"' + $SiteTitle + '","Url":"' + $SiteUrl + '","Description":"","Classification":"","AllowFileSharingForGuestUsers":false}}' }

Get a user based context for SharePoint (app credentials not supported for this approach)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantUrl) $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $(ConvertTo-SecureString -AsPlainText $Password -Force)) $Context.ExecuteQuery()

Get url, cookie and forms digest for authentication

$RequestUrl = "$($TenantUrl)/_api/sitepages/publishingsite/create" $AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($TenantUrl, $true) $FormsDigest = $Context.GetFormDigestDirect()

$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession $WebSession.Credentials = $Context.Credentials $WebSession.Cookies.SetCookies($TenantUrl, $AuthenticationCookie)

$Headers = @{ 'X-RequestDigest' = $FormsDigest.DigestValue; 'accept' = 'application/json;odata=verbose'; 'content-type' = 'application/json;odata=verbose' }

Call REST API to create new site

$Result = Invoke-RestMethod -Method Post -WebSession $WebSession -Headers $Headers -ContentType "application/json;odata=verbose;charset=utf-8" -Body $RequestBody -Uri $RequestUrl -UseDefaultCredentials


Site has been created

Write-Output "New site created at: $($Result.d.Create.SiteUrl)"

  • I take this script from a site I can´t remember I´m trying to find it now
    – user70011
    Apr 3, 2018 at 17:26

You get the error because “Get-SPWeb” is for SharePoint On-premise environment.

In SharePoint Online, you can use the script below to find all checked out file and check in them:

function Get-SPOFolderFiles
param (

  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
 $spqQuery.ViewXml ="<View Scope='RecursiveAll' />";

  foreach($item in $itemki)

  Write-Host $item["FileRef"] $item.ElementType

  $file =


       if($CheckedOutByUser.LoginName -ne $null){
       Write-Host $file.Name
       Write-Host $CheckedOutByUser.LoginName
       $file.CheckIn('Checked in automatically', 'MajorCheckIn')
  Write-Host $file.Name " has been checked in"     -ForegroundColor DarkGreen 
        catch [Net.WebException]
        Write-Host $_.Exception.ToString()



#Paths to SDK
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

#Enter the data
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$username="[email protected]"

Get-sPOFolderFiles -Username $username -Url $Url -password $AdminPassword -ListTitle $ListTitle

More information and PowerShell script file download in the article below: https://gallery.technet.microsoft.com/office/Find-all-checked-out-files-56fd562d#content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.