This PowerShell SharePoint tutorial, we will see how to create an encrypted password file using PowerShell and then we use the password from the encrypted file in SharePoint Online.
Recently I got a requirement to work to copy files from a shared drive to SharePoint online document library using PowerShell. In the PowerShell, we need to pass credentials to connect to the SharePoint Online site. Instead of passing in clear text, we wanted to pass an encrypted string.
You can write in Windows PowerShell ISE or also you can use Visual Studio Code to write and debug PowerShell Script.
Create Encrypted Password File in PowerShell
You can use one of the below ways to create an encrypted password file in PowerShell. The best way to run the commands through PowerShell ISE.

Approach-1:
$mypassword="MyPassword"
$secureStringPassword = $mypassword | ConvertFrom-SecureString
Set-Content "C:\Bijay\Password.txt" $secureStringPassword
Approach-2:
"MyPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Bijay\Password.txt"
After you run any of the above PowerShell commands. It will generate the encrypted password file inside C:\Bijay folder with name as Password.txt.
How to use PowerShell encrypted password file?
Once we create the encrypted file using above steps we can use it like below:
$securePassword = Get-Content "C:\Bijay\Password.txt" | ConvertTo-SecureString
Then you can use the $securePassword variable where ever you want like below:
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("myusername@xxxxx.onmicrosoft.com", $securePassword)
You may like following PowerShell tutorials:
- Retrieve all list names and list guids from SharePoint online site using PowerShell
- PowerShell Basics: A Beginners Guide
- Working with PowerShell Date Command (Get-Date)
- PowerShell reference variable
- What is a PowerShell variable
- SharePoint 2013 backup and restore using PowerShell
- PowerShell find files modified in last N days
- How to check if file created last 24 hours using PowerShell?
- How to create and use PowerShell global variable
- Create folder if not exists using PowerShell
- Create file if not exists with name as today’s date using PowerShell
- How to create an array in PowerShell from CSV file
This PowerShell tutorial, we learned how to create an encrypted password file using PowerShell and also, we will see how to use the same PowerShell encrypted password file in SharePoint Online.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com