How to always return array object in PowerShell?

Summary

I have a PowerShell Script, I get all items from the SharePoint list. After getting all items I want to filter by a condition so I used the Where-Object as following.

# fyi, % -> ForEach-Object
[array]$items = Get-PnPListItem @HashArguments | `
  %{New-Object psobject -Property `
  @{ 
    Id                          = $_.Id; 
    reqStatus                   = $_["reqStatus"];
  }} | `
  select ID, 
    reqStatus

[array]$PendingFilteredItems  = $items | Where-Object {$_.reqStatus -contains 'Active'}  | Sort-Object -Property Id

if ( ($PendingFilteredItems -ne $null) -and ($PendingFilteredItems.Count -gt 0) )
{
   # ... further processing for array count is greater than zero
}

All worked fine, the condition was hitting for greater than zero items. But issue came when the item left in the SharePoint list as one item. I always got a “PSCustomObject” as a type for the PendingFilteredItems.

How to always return and array for any number of items left?

Basically I had to add an [array] type for the variable. It is highlighted in the above code.

Conclusion

I know it is simple post for such a solution. If you read this and fix the issue at your side this will save lot of time. I will be happy that you saved some time.

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s