When you sharing PnP PowerShell scripts with someone, sometimes these users don’t have the option to install cmdlets on their machines and that will lead to some questions back and forth about the script not working as expected. So, we can save the modules required to perform our task on the PC and then execute from there the code.
For this script to work, you need to know the version of the PnP PowerShell that you need to install. So with the following cmdlet, you can get the version that is running on your desktop. If you want to specify other versions, check here.
(Get-Module SharePointPnPPowerShellOnline -ListAvailable)[0].Version.ToString()
Now you just need to replace the SharePoint site collection where you need to connect and do the code that you want to do. In this case, we will show all the lists available, on the SharePoint site, that are visible. When the script concludes all the actions, we’ll delete the folder.
$siteCollectionUrl = "https://contoso.sharepoint.com/sites/site"
$version = "3.18.2002.0"
$CurrentDir = $(Get-Location).Path;
Save-Module -Name SharePointPnPPowerShellOnline -Path $CurrentDir -RequiredVersion $version
Import-Module ("{0}\SharePointPnPPowerShellOnline\{1}\SharePointPnPPowerShellOnline.psd1" -f $CurrentDir,$version) -DisableNameChecking
Connect-PnPOnline -Url $siteCollectionUrl -UseWebLogin
Get-PnPList | Where-Object {!$_.Hidden}
Disconnect-PnPOnline
Remove-Item -Path ("{0}\SharePointPnPPowerShellOnline" -f $CurrentDir) -Recurse -Force
Conclusion
Take into consideration the person that you’ll send the code and adding this little script can avoid:
- User doesn’t have the PnP Powershell installed
- The version installed, doesn’t have the required cmdlets
- The version installed have an issue on the cmdlet.
[…] Run PnP PowerShell with saved module – SharePoint Tricks […]
Another thing in the script I would say, check the execution policy and set it to remote/unrestricted before running the script. Once the script execution completed, set it back to the original value at the end.
Just a suggestion.
Ravi
With this import method, some cmdlets doesn’t work, for example the new “Initialize-PnPPowerShellAuthentication”. It returns me a “Could not load file or assembly ‘System.ValueTuple,version=4.0.2.0[…]’ or one of its dependencies”…
Do you have any clues about this ?
Hi,
Check if you have a folder with the PnP Powershell on the location. Are you using which version of the PowerShell?
My best,
David Ramalho