Skip to main content

Avoid repeatedly entering your credentials with the browser and modern connection methods

Connect once in a browser window when you start your PowerShell script, and then PowerShell reuses that connection for every following connection in the loop

Updated over a week ago

To avoid entering your credentials for every connection in a foreach loop statement with Browser authentication and Modern authentication, you can use the PowerShell parameter -UseCredentialsFrom to create a single connection and reuse the credentials from that connection in your script.

-UseCredentialsFrom

To illustrate how the -UseCredentialsFrom works, here is a basic connection script:

# Connect to the first SharePoint site using browser authentication and store the connection

$connection = Connect-Site -Url "http://mytenant/sites/mysitecollection1" -ModernAuth

# Use the stored credentials to connect to the second SharePoint site

Connect-Site -Url "http://mytenant/sites/mysitecollection2" -UseCredentialsFrom $connection

# Use the same stored credentials to connect to the third SharePoint site

Connect-Site -Url "http://mytenant/sites/mysitecollection3" -UseCredentialsFrom $connection

When you run this script, a browser window will open so you can connect to mysitecollection1. Once the connection is established, the script will automatically use that first connection to connect to mysitecollection2 and mysitecollection3.

How to use -UseCredentialsFrom with a foreach loop

A foreach loop statement typically uses brackets to repeat connection and copy commands for each row of a CSV file with the app. Here is an example:

# Import the Sharegate module
Import-Module ShareGate

# Define the path to the CSV file containing site information
$csvFile = "C:\CSV\CopySites.csv"

# Import the CSV file into a table using a comma as the delimiter
$table = Import-Csv $csvFile -Delimiter ","

# Define source site credentials
$srcUsername = "sourceusername"
$srcPassword = ConvertTo-SecureString 'sourcepassword' -AsPlainText -Force

# Connect to the destination site using browser authentication and store the connection
$dstsiteConnection = Connect-Site -Url "https://tenantname.sharepoint.com/sites/sitename" -Browser

# Declare variables for source and destination sites
Set-Variable -Name srcSite
Set-Variable -Name dstSite

# Loop through each row in the CSV table and copy sites from source to destination
foreach ($row in $table) {

# Clear any existing variable values for source and destination sites
Clear-Variable -Name srcSite
Clear-Variable -Name dstSite

# Connect to the source site using credentials
$srcSite = Connect-Site -Url $row.SourceSite -Username $srcUsername -Password $srcPassword

# Connect to the destination site using the stored credentials from the browser connection
$dstSite = Connect-Site -Url $row.DestinationSite -UseCredentialsFrom $dstsiteConnection

# Copy the source site to the destination site with merging and subsites included
Copy-Site -Site $srcSite -DestinationSite $dstSite -Merge -Subsites
}

When you run this script, you are prompted to enter your credentials in a browser window before the foreach statement.

The connection is stored in the $dstsiteConnection variable, which is reused on each pass of -UseCredentialsFrom $dstsiteConnection in the loop.

This ensures you do not need to re-enter your credentials for every site being copied.

Note: Avoid using any of the URLs listed in your CSV for the $dstsiteConnection variable. If you use one of your listed URLs, the Clear-Variable command will clear your credentials.

Did this answer your question?