Office PnP PowerShell: How to dispose SPWeb from Get-PnPSubWebs

%3CLINGO-SUB%20id%3D%22lingo-sub-46281%22%20slang%3D%22en-US%22%3EOffice%20PnP%20PowerShell%3A%20How%20to%20dispose%20SPWeb%20from%20Get-PnPSubWebs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-46281%22%20slang%3D%22en-US%22%3E%3CP%3EHaving%20foreach%20loop%20with%26nbsp%3BGet-PnPSubWebs%2C%20how%20do%20I%20dispose%20SPWebs%20at%20the%20end%20of%20loop%20cycle%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-46438%22%20slang%3D%22en-US%22%3ERe%3A%20Office%20PnP%20PowerShell%3A%20How%20to%20dispose%20SPWeb%20from%20Get-PnPSubWebs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-46438%22%20slang%3D%22en-US%22%3E%3CP%3ESo%20like%20this%3F%3C%2FP%3E%3CPRE%3Eforeach%20(%24web%20in%20Get-PnPSubWebs)%0A%7B%0A%26nbsp%3B%20%26nbsp%3B%20%23Do%20something%20with%20%24web%0A%26nbsp%3B%20%26nbsp%3B%20%24web.Context.Dispose()%20%23%20%26lt%3B%26lt%3B%26lt%3B%26lt%3B%26lt%3B%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-46432%22%20slang%3D%22en-US%22%3ERE%3A%20Office%20PnP%20PowerShell%3A%20How%20to%20dispose%20SPWeb%20from%20Get-PnPSubWebs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-46432%22%20slang%3D%22en-US%22%3EI%20think%20this%20thread%20will%20help%20you%20%3CA%20href%3D%22http%3A%2F%2Fstackoverflow.com%2Fquestions%2F1999858%2Fhow-bad-is-it-to-not-dispose-in-powershell%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F1999858%2Fhow-bad-is-it-to-not-dispose-in-powershell%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-46428%22%20slang%3D%22en-US%22%3ERe%3A%20Office%20PnP%20PowerShell%3A%20How%20to%20dispose%20SPWeb%20from%20Get-PnPSubWebs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-46428%22%20slang%3D%22en-US%22%3EI%20may%20not%20understand%20your%20question%20correctly.%20Dispose%20is%20available%20only%20through%20Context.%20You%20can%20post%20the%20sample%20to%20provide%20you%20the%20proper%20feedback.%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Having foreach loop with Get-PnPSubWebs, how do I dispose SPWebs at the end of loop cycle?

3 Replies
Highlighted
I may not understand your question correctly. Dispose is available only through Context. You can post the sample to provide you the proper feedback.
Highlighted
Highlighted

So like this?

foreach ($web in Get-PnPSubWebs)
{
    #Do something with $web
    $web.Context.Dispose() # <<<<<
}