SOLVED

Connect-PnPOnline only once for command line?

Iron Contributor

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?  Is there a way to reuse that connection via the command line?

3 Replies
best response confirmed by LucidBike1140 (Iron Contributor)
Solution

@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!

@Don Kirkham 

Thanks for the explanation!  I think the issue has been my confusion over PnP PowerShell and SharePoint PnP PowerShell Online.  I was connecting with PnP-Connect but then trying to use  SharePoint PnP PowerShell Online commands 🤦🏻‍:male_sign:.  Since PnP PowerShell is a replacement, I've been trying to use that as much as possible, but then I come across commands for the other one (or there isn't a replacement command) and I don't realize I'm mixing PowerShells.  After realizing this, I can now tell the difference between the commands.

 

Thank you for explaining the -ReturnConnection parameter.  I will certainly be using that going forward.

 

-Steve

I wish the PnP connection could be used by other libraries, but not available yet. I agree that I use PnP as much as possible!!
1 best response

Accepted Solutions
best response confirmed by LucidBike1140 (Iron Contributor)
Solution

@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!

View solution in original post