SOLVED

Connect-PnPOnline only once for command line?

%3CLINGO-SUB%20id%3D%22lingo-sub-2264089%22%20slang%3D%22en-US%22%3EConnect-PnPOnline%20only%20once%20for%20command%20line%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2264089%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20learned%20how%20to%20store%20my%20credentials%20securely%20and%20use%20them%20in%20a%20script%2C%20but%20is%20there%20a%20way%20I%20can%20use%20Connect-PnPOnline%20at%20the%20command%20line%20so%20subsequent%20commands%20I%20type%20use%20the%20same%20connection%3F%26nbsp%3B%20Is%20there%20a%20way%20to%20reuse%20that%20connection%20via%20the%20command%20line%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2264089%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional 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 Steve Wright (Occasional Contributor)
Solution

@Steve Wright 

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