Forum Discussion

LucidBike1140's avatar
LucidBike1140
Iron Contributor
Apr 09, 2021
Solved

Connect-PnPOnline only once for command line?

I've learned how to store my credentials securely and use them in a script, but is there a way I can use Connect-PnPOnline at the command line so subsequent commands I type use the same connection?  ...
  • Don Kirkham's avatar
    Apr 12, 2021

    LucidBike1140 

    The Connect-PnpOnline cmdlet sets the resulting connection as the active connection. It will be used for subsequent PnP cmdlets by default until the connection is closed via the Disconnect-PnPOnline cmdlet or another Connect-PnPOnline cmdlet is executed.

     

    You can also store the connection in a variable using the -ReturnConnection parameter in the Connect-PnPConnect cmdlet or the Get-PnPConnection cmdlet. Most PnP cmdlets have an optional -Connection parameter where you can use the variable where the connection is stored. I find this useful if I need to switch to a different connection for part of a script, but come back to the original connection later in the script.

     

    The full documentation for PnP PowerShell can be found at https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp 

     

    If this answers your question, please mark it as Accepted solution! Have a great day!

Resources