Apr 09 2021 07:42 AM - edited Apr 09 2021 07:58 AM
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?
Apr 12 2021 04:10 AM
SolutionThe 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!
Apr 12 2021 08:09 AM
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
Apr 12 2021 08:28 AM
Apr 12 2021 04:10 AM
SolutionThe 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!