I have successfully been using some CSOM based scripts, but suddenly the CSOM parts are no longer working.

For example, when executing the query below it errors with "(401) Unauthorized".

# Create internal and external document libraries
$listTitle = "Internal Files"

$listDescription = "Files stored in this library are only available to internal users only."
$listTemplate = 101

# Set SharePoint Online Credentials
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

# Creating client context object
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $SPOCredentials

# Create Internal 
$lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$lci.title = $listTitle
$lci.description = $listDescription
$lci.TemplateType = $listTemplate
$list = $context.web.lists.add($lci)

# Send the request containing all operations to the server
    Write-Host "Created $($listTitle) library" -ForegroundColor Green
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red

Of course I wondered if something had happened with the credentials, but I've tested them and it works. In fact, further up the script we do a Connect-SPOService using the same creds and it connects fine.

I can also run other scripts using the same credentials, but they are SPO commands not CSOM.

To confirm, the user account is an SPO admin.

Is there anything else I can check?


  • How do you enter your password? Do enter it as an securestring? Aug 15 '18 at 14:08

My guess is the $SPOCredentials object is improperly formed. The $password argument needs to be a SecureString. Are you using Convert-ToSecureString on that somewhere upstream in the script?

Also, Check that your user account actually has access to the site. Just being an admin doesn't mean you have access to all the sites. Of course, you can grant yourself access.

One more thing - you really should consider using the PnP PowerShell CmdLets. They abstract away all the things that are giving you these problems. You can still retain the option to drop into CSOM - just run a Get-PnPContext and you've got your context object.

  • Thanks, but indeed it is coming from an encrypted file: $keyfile = "file.key" $key = Get-Content $keyfile $encrypted = "file.txt" $userName = "username@something.com" $password = Get-Content $encrypted | ConvertTo-SecureString -Key $key Also it sets the service account running the script as the owner when creating the site in the first place. I will look at PnP when I have more time - but the point here is that this has suddenly stopped working, and it all worked fine just last week. Thanks
    – Tom Urwin
    Aug 15 '18 at 14:30
  • 1
    If you can run other scripts using the same creds and connection code, the clue to your issue would be to figure out the difference there. Aug 15 '18 at 14:35
  • Things like Connect-SPOService and New-SPOSite are working with those credentials, but nothing CSOM wise works anymore that I can see. I wonder if MS could have changed something?
    – Tom Urwin
    Aug 15 '18 at 14:40
  • @TomUrwin ... Could you please check if your file path reference is correct ... ideally you need to provide the path for the same folder using "./file.key" or "./file.txt" or provide the complete path for Get-Content. Aug 15 '18 at 15:16
  • 1
    Almost figured it out! For some reason when running the New-SPOSite command, it is not setting the Owner correctly (even when running the command separately). Looking into that now, but it explains why it doesn't have access to the site. Thanks for your time.
    – Tom Urwin
    Aug 15 '18 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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