What I would like is to logon to sharepoint with powershell using the CSOM-module and using the Credential Manager where I have safely stored the password.

This is a working code to logon to sharepoint

          $Credentials = New-Object 
          Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,(ConvertTo-SecureString $pass -AsPlainText -Force))
           $Context =  New-Object 
            $Context.Credentials = $Credentials

But how can I use the credential manager. I have installed the module en made this:

#$managedCred = Get-StoredCredential -Target SharepointOnline

Does anyone know the solution.

Gr P

2 Answers 2


Peter, I have answered the same question posted by you in another Technet Thread, please remember to check it:

[TechNet] Log on to SharePoint with Powershell using the Credential Manager

Hi PeetK,

Firstly, create a Generic Credentials in Credential Manager:

enter image description here

In my side, the name is "o365" for example, then in PowerShell, use like this:

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"
$cred = Get-StoredCredential -Target 'o365'  
#Variables for Processing
$SiteUrl = "https://TenantName.sharepoint.com/sites/SiteName/"

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)

#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials

enter image description here

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Gaurravs
    Nov 5, 2019 at 9:02
  • @Gaurravs, OK, I will update my reply above to include detailed content.
    – Jerry
    Nov 5, 2019 at 10:48
  • 1
    @Gaurravs,Done, please check.
    – Jerry
    Nov 5, 2019 at 11:00

This is possible with PnP PowerShell. I use this all the time as I have dozens of client environments I connect to.

Connect-PnPOnline <url> -Credential <name of stored credential>


If you want to do this without PnP you can check out the code on Github and reverse engineer it.

Your Answer

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