Powershell - combining arrays
arrays, powershell
Solution
If `Import-Csv` only returns 1 result, then you are correct that your variable is assumed NOT to be an array, then concatenation will fail. This is not change by the fact that you have pre-initialized your variables with `@()`. In fact, that step isn't necessary.
To force the result to be treated as an array, you can either wrap your whole `Import-Csv` line in `@()`, or do something similar afterward.
$new = @( Import-Csv $File | Where-Object {...} )
# or
$new = Import-Csv $File | Where-Object {...}
$new = @($new)
Problem
I am new to powershell and in need of help. My first script for work is automate the new and termed users in AD environment. A CSV dump will be done once daily from our Peoplesoft system. I use `Import-CSV` and create 3 arrays (new, term and processed). The trouble I'm having is with combining the 3 arrays once i loop through all the users and try putting it back into the file. The code breaks at the `$New += $Term` lines. I believe this is due to the fact that there is only 1 record of each user type (new, term and processed) in my test file (I know, add more users…can't. This may be a real world outcome for any particular day). Below is my sample code: ``` #Get Credentials from user $c = Get-Credential #Get Date for $Term array population $e = Get-Date -format M/d/yyyy #Set file location and variable for said file $File = "c:\users\nmaddux\desktop\adduserstuff\test.csv" #Import record sets for New and Term users $New = @() $Term = @() $Procd = @() $New = Import-Csv $File | Where-Object { $_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq "" } $Term = Import-Csv $File | Where-Object { $_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e } $Procd = Import-Csv $File | Where-Object { $_.Processdate -ne "" } #Process both new and term users provided there are records to process for each If ($New -ne $NULL -and $Term -ne $NULL) { # Some code to process users } $new += $term $new += $Procd $new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue ``` So it will export but only partial results. error - Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.