Create an encrypted password file in PowerShell and use in SharePoint online

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.

PowerShell Create encrypted file and use in SharePoint Online
PowerShell Create encrypted file and use in SharePoint Online

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:

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.

>