Introduction
This article will describe different ways of managing the credentials for SharePoint online using PowerShell.
We can pass the credentials in three different ways.
Using Secured String
In this type, we are hardcoding the username and password and then passing it for authentication.
Disadvantage
The password is visible to everyone who has access to the PowerShell script.
Using File
By using this file, we can secure our credentials a little bit, as the password is stored in encrypted format.
Disadvantage
The secret file is stored at the local drive on our computer and can be decrypted using some tools.
Using Window Credential Manager.
This type is the most secured and recommended way to pass the credentials.
Disadvantage
In this type, the user should have admin right, otherwise, it will prompt for credentials everytime we run the script.
Using Secured String
Hardcode the credentials in the following variables:
- $Username="Your userName"
- $Password="Your Password"
The complete code will look like:
- Function Login-SharePointOnline()
- {
- Try {
- #Get Credentials to connect
- $Username="Your userName"
- $Password="Your Password"
- $ListName="List Name"
- $siteURL="Site Url"
- $query = "<View>
- </View>"
- $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
- $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword)
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $Ctx.Credentials = $Cred
- #check context available or not
- if (-not($Ctx)) {
- Write-Host "Error connecting to SharePoint Online, unable to establish context" -foregroundcolor black -backgroundcolor Red
- return
- }
- else
- {
- Write-Host "Connected to the Sharepoint Online successfully" -foregroundcolor green
- }
- #Get CAML Query object
- $camlquery = New-Object Microsoft.SharePoint.Client.CamlQuery;
- $camlquery.ViewXml= $query
- $List = $Ctx.Web.Lists.GetByTitle($ListName)
- $Items = $List.GetItems($camlquery)
- $Ctx.Load($Items)
- $Ctx.ExecuteQuery()
- #Get each value from list and print
- ForEach($Item in $Items)
- {
- Write-Host "Item in the List is:"$Item["Title"]
- }
- Write-Host $SourceList
- }
- catch
- {
- Write-Host "Error connecting to SharePoint Online: $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
- return
- }
- }
- #Calling function
- Login-SharePointOnline
Using File
Follow the below steps to create a secret file and write the username and password into that file.
Step 1
I have created folder Arvind\safe under (D:\) drive. (You can create your own directory to save the secretfile).
Step 2
Open powershell window run as administrator and run the following script which will prompt for credentials.
- $credentials = Get-Credential
- $filename = 'D:\Arvind\safe\secretfile.txt'
- $credentials | Export-Clixml -path $filename

Provide your username and password and click the OK button to generate the secret file.
Ensure that the secret file is generated at our directory (D:\Arvind\safe\) and that it contains the username and password (encrypted format).
Step 3
To read and pass the credentials to sharepoint online, include the following lines in the script:
- $credPath = 'D:\Arvind\safe\secretfile.txt'
- $fileCred = Import-Clixml -path $credpath
- $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($fileCred.UserName, $fileCred.Password)



Vinay AyinapurapuPosted Jun 17, 2021, 2:04 PM
Very nice Arvind.. Thanks for detailed article.
Andries JoossePosted Jul 10, 2020, 6:13 AM
Oh and by "without user interaction" I mean without having to type an username / password. It would be great if it could just pass that on from the current logged on user (like some sort of intergrated security).
Andries JoossePosted Jul 10, 2020, 5:33 AM
Hello! Great article and explanation, thank you so much! Just a question. I'm trying to download a file to a local computer by using a Powershell script. The file is on a SharePoint Online library. The goal is to download the file without any user interaction. Do you think this would be possible in any way? The computer is Intune managed by the way, so we can push all sorts of configuration and scripts to it. Thank you so much in advance!