6

In Sitecore Powershell extensions I can use

If ( Get-Item ...)

to return true if an item exists or false if it doesn't, but if the item isn't found then it outputs an error message as well.

Is there a way of checking for an item's existence that doesn't output an error message (and is possibly less intensive than using Get-Item - I only need to know if it's there or not, I don't need to instantiate it)? I've tried using the standard powershell -ErrorAction SilentlyContinue but this doesn't seem to be implemented.

9

Try this

$database = "master"
$homeItem = "$($database):\sitecore\content\home123"
$homeExists = Test-Path -Path $homeItem
6

An alternative would be to run the Get-Item command and provide the necessary ErrorAction parameter to hide the output.

# Variable $item will contain the object if found.
if(($item = Get-Item -Path "master:\content\home1" -ErrorAction SilentlyContinue)) {
    $item
} else {
    Write-Host "Does not exist."
}

Sometimes you may also see the parameter shortened to -ea 0.

  • That was what I was trying, but I still seemed to get output, though I wasn't trying to read into an item. I'll try again just in case I had some syntax error. – James Walford Sep 18 '17 at 15:00
  • I must have had a typo, because SilentlyContinue is working now - thanks! – James Walford Sep 18 '17 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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