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.
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.
$mypassword="MyPassword" $secureStringPassword = $mypassword | ConvertFrom-SecureString Set-Content "C:\Bijay\Password.txt" $secureStringPassword
"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("firstname.lastname@example.org", $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