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%3CLINGO-SUB%20id%3D%22lingo-sub-2267717%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPOnline%20only%20once%20for%20command%20line%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267717%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F232897%22%20target%3D%22_blank%22%3E%40Steve%20Wright%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Connect-PnpOnline%20cmdlet%20sets%20the%20resulting%20connection%20as%20the%20active%20connection.%20It%20will%20be%20used%20for%20subsequent%20PnP%20cmdlets%20by%20default%20until%20the%20connection%20is%20closed%20via%20the%20Disconnect-PnPOnline%20cmdlet%20or%20another%20Connect-PnPOnline%20cmdlet%20is%20executed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20also%20store%20the%20connection%20in%20a%20variable%20using%20the%20-ReturnConnection%20parameter%20in%20the%20Connect-PnPConnect%20cmdlet%20or%20the%20Get-PnPConnection%20cmdlet.%20Most%20PnP%20cmdlets%20have%20an%20optional%20-Connection%20parameter%20where%20you%20can%20use%20the%20variable%20where%20the%20connection%20is%20stored.%20I%20find%20this%20useful%20if%20I%20need%20to%20switch%20to%20a%20different%20connection%20for%20part%20of%20a%20script%2C%20but%20come%20back%20to%20the%20original%20connection%20later%20in%20the%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20full%20documentation%20for%20PnP%20PowerShell%20can%20be%20found%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20this%20answers%20your%20question%2C%20please%20mark%20it%20as%20Accepted%20solution!%20Have%20a%20great%20day!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2268046%22%20slang%3D%22en-US%22%3ERe%3A%20Connect-PnPOnline%20only%20once%20for%20command%20line%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2268046%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F139606%22%20target%3D%22_blank%22%3E%40Don%20Kirkham%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20explanation!%26nbsp%3B%20I%20think%20the%20issue%20has%20been%20my%20confusion%20over%20%3CSTRONG%3EPnP%20PowerShell%3C%2FSTRONG%3E%20and%26nbsp%3B%3CSTRONG%3ESharePoint%20PnP%20PowerShell%20Online%3C%2FSTRONG%3E.%26nbsp%3B%20I%20was%20connecting%20with%20PnP-Connect%20but%20then%20trying%20to%20use%26nbsp%3B%20SharePoint%20PnP%20PowerShell%20Online%20commands%20%F0%9F%A4%A6%F0%9F%8F%BB%E2%80%8D%3Amale_sign%3A.%26nbsp%3B%20Since%26nbsp%3BPnP%20PowerShell%20is%20a%20replacement%2C%20I've%20been%20trying%20to%20use%20that%20as%20much%20as%20possible%2C%20but%20then%20I%20come%20across%20commands%20for%20the%20other%20one%20(or%20there%20isn't%20a%20replacement%20command)%20and%20I%20don't%20realize%20I'm%20mixing%20PowerShells.%26nbsp%3B%20After%20realizing%20this%2C%20I%20can%20now%20tell%20the%20difference%20between%20the%20commands.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20explaining%20the%26nbsp%3B%3CSPAN%3E%3CSTRONG%3E-ReturnConnection%3C%2FSTRONG%3E%20parameter.%26nbsp%3B%20I%20will%20certainly%20be%20using%20that%20going%20forward.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E-Steve%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
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 (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!!