I am trying to export some data from sharepoint list to csv and I got this error:
$ListItemCollection | Export-CSV \"D:\\LX.csv\" -NoTypeInformation
tl;dr:
$ListItemCollection
is of type [System.Management.Automation.PSObject]
, not an array.
Make sure that it is an array (e.g., $ListItemCollection = @()
) for +=
to work as intended, i.e., for +=
to append an element[1].
Note that commands that typically output multiple items - which are then collected in a regular [object[]]
array, if assigned to a variable - output only a scalar if the command situationally happens to return only one item; in other words: a single-item output array is automatically unwrapped.
Therefore, if there's a chance that a command situationally returns only a single object, yet you need the result to always be an array, use @(...)
, the array-subexpression operator; e.g.:
# @(...) ensures that $file is an array, even if just 1 file matches
$files = @(Get-ChildItem *.txt)
The error message implies that $ListItemCollection
is of type [System.Management.Automation.PSObject]
and not an array.
Since type [pscustomobject]
([System.Management.Automation.PSObject]
) does not have a static op_Addition
method, you cannot use the +
operator with an instance of it as the LHS.
(Type-specific operators are implemented as static op_*
methods).
You can verify this as follows:
PS> (New-Object System.Management.Automation.PSObject) + 1 # !! Breaks
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'
If you want to inspect a given type for operator support, use a command such as the following, using the [datetime]
type as an example:
PS> [datetime] | Get-Member -Force -Static -Type Method op_*
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
op_Addition Method static datetime op_Addition(datetime d, timespan t)
op_Equality Method static bool op_Equality(datetime d1, datetime d2)
op_GreaterThan Method static bool op_GreaterThan(datetime t1, datetime t2)
op_GreaterThanOrEqual Method static bool op_GreaterThanOrEqual(datetime t1, datetime t2)
op_Inequality Method static bool op_Inequality(datetime d1, datetime d2)
op_LessThan Method static bool op_LessThan(datetime t1, datetime t2)
op_LessThanOrEqual Method static bool op_LessThanOrEqual(datetime t1, datetime t2)
op_Subtraction Method static datetime op_Subtraction(datetime d, timespan t), static timespan op_Subtraction(datetime d1, datetime d2)
Note:
The "primitive" .NET data types do not have such methods, because operator support for them is built in.
Similarly, it is PowerShell itself that implements +
for arrays and collections ([object[]]
, [System.Collections.Generic.List[object]]
, ...), though note that:
[object[]]
(unless you use a type-constrained variable that converts the array back to a different collection type).-Force
is needed, because Get-Member
hides the op_*
methods by default.
[1] Technically, a new array is created behind the scenes, because arrays are immutable. In loops this can be a performance concern; if so, use a mutable collection type such as [System.Collections.Generic.List[object]]
and append to it with its .Add()
method.