1

I am working with SharePoint PnP PowerShell and using below command. This command works but when the site does not exist it gives me an error on the screen even if I specified -ErrorAction:SilentlyContinue.

$objSite = Get-SPOSite -Identity $siteUrl
2
  • 1
    Get-SPOsite is not a pnp powershell command. Pnp commands have‘pnp’ phrase in it.. and yes, its a bug that -erroraction : silently continue doesn’t work mostly
    – DvG
    Commented Jul 9, 2018 at 15:00
  • Thanks for correcting me. What I mean was working with PnP Powershell with SharePoint PowerShell command. Is there any replacement for "Get-SPOSite" command so that I could ignore the error in case if the site does not exist. Commented Jul 9, 2018 at 16:28

1 Answer 1

3

There are 2 ways you can avoid such cases, 1. Use Try Catch block Reference msdn

try{ Get-SPOSite -Identity $siteUrl }
Catch { write-host “error in finding the site”}

2. Change the logic and use filter to handle the null variable something like this

$objSite = Get-SPOSite -Filter {Title -eq “XYZ”} 

This way if the site title is not found, you will end up with null variable rather than an powershell error message. Null variable can easily be handled with IF conditions.

Cheers! Happy to help.

4
  • Do we really need "-errorAction silentlyContinue" on approach# 2? Commented Jul 9, 2018 at 17:31
  • Not required, I will update my answer .. thanks for pointing out
    – DvG
    Commented Jul 9, 2018 at 17:34
  • 1
    Thanks a lot. Because I don't want to use Title as a filter so tried with Get-SPOSite -Filter {Url -eq $siteUrl} but did not work so finally I used Get-SPOSite | Where-Object Url -eq $siteUrl which worked. Commented Jul 9, 2018 at 19:06
  • Perfect, My intention was to share the idea, you can decide the parameters based on your code’s requirement..
    – DvG
    Commented Jul 9, 2018 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.