Skip to main content
Hide credentials in scripts
Updated this week

Command

Get-Credential

Details

If you need to use manual mode to connect to your SharePoint site, you will need to enter your credentials in the script. This can be a concern since anyone that has access to the script will be able to see your credentials. Use this PowerShell alternative with the Connect-Site command.

How-to

$credentials = Get-Credential

This will show a window where you can enter the credentials for this execution of the script.

You can then use those credentials to connect to your site:

$credentials = Get-Credential
$site = Connect-Site -Url "http://farm/sites/siteCollection" -Credential $credentials

Using this technique, you will never expose your credentials in a script. This way, you will need to enter manually your credentials for every execution of the scripts which makes scheduling very difficult. It is possible to output the secure string generated by PowerShell for your password. It is not possible for a user to decrypt your password this way but someone could take the encrypted password and use it to do something else. Anyway, it is possible, and here is how. You need to first create a small script to get the encrypted password.

$credentials = Get-Credential
ConvertFrom-SecureString $credentials.Password
01000000d08c9ddf0115d1118c7a00c04fc297eb01000000c57ba489a6781343983ef0deba79d0250000000002000000000003660000c0000000100000007ba4ea450748178832c0187a37c513270000000004800000a000000010000000a3fe44ced5ac805a2fcf7a6c3652994510000000ccc7701593bd519546d09f216962fac4140000003904619dc8c320b7fb0cafa6efdd78dd2c69c8e5

Then, you can copy this string which is your password encrypted, and put it in your script like this to connect to your site:

$secureString = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000c57ba489a6781343983ef0deba79d0250000000002000000000003660000c0000000100000007ba4ea450748178832c0187a37c513270000000004800000a000000010000000a3fe44ced5ac805a2fcf7a6c3652994510000000ccc7701593bd519546d09f216962fac4140000003904619dc8c320b7fb0cafa6efdd78dd2c69c8e5"
$password = ConvertTo-SecureString $secureString
$username = [email protected]
Connect-Site -Url "http://farm/sites/siteCollection" -Username $username -Password $password

As you can see, your password will not be shown in plain text, but keep in mind that it would still be possible to copy the secure string and use it in another script. However, since secure strings are generated using your local machine encryption key and the current user's permissions, they can only be used on the same machine and cannot be transferred to a script running on a different machine.

Did this answer your question?