Forum Discussion
Connect-PnPOnline only once for command line?
- Apr 12, 2021
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!
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!
- LucidBike1140Apr 12, 2021Iron Contributor
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
- Don KirkhamApr 12, 2021MVPI wish the PnP connection could be used by other libraries, but not available yet. I agree that I use PnP as much as possible!!