Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:37:38+00:00 2026-06-14T17:37:38+00:00

After reading this helpful article on the Windows PowerShell Blog, I realized I could

  • 0

After reading this helpful article on the Windows PowerShell Blog, I realized I could “shift” off the first part of an array, but didn’t know how to “unshift” or push elements onto the front of an array in PowerShell.

I am creating an array of hash objects, with the last read item pushed onto the array first. I’m wondering if there is a better way to accomplish this.

## Create a list of files for this collection, pushing item on top of all other items
if ($csvfiles[$collname]) {
    $csvfiles[$collname] = @{ repdate = $date; fileobj = $csv }, $csvfiles[$collname] | %{$_}
}
else {
    $csvfiles[$collname] = @{ repdate = $date; fileobj = $csv }
}

A couple of things to note:

  1. I need to unroll the previous array element with a foreach loop %{$_}, because merely referencing the array would create a nested array structure. I have to have all the elements at the same level.
  2. I need to differentiate between an empty array and one that contains elements. If I attempt to unroll an empty array, it places a $null element at the end of the array.

Thoughts?

PS: The reason the empty hash element produces a NULL value is that $null is treated as a scalar in PowerShell. For details, see https://connect.microsoft.com/PowerShell/feedback/details/281908/foreach-should-not-execute-the-loop-body-for-a-scalar-value-of-null.

ANSWER:

Looks like the best solution is to pre-create the empty array when necessary, rather than code around the $null issue. Here’s the rewrite using a .NET ArrayList and a native PoSh array.

if (!$csvfiles.ContainsKey($collname)) {
    $csvfiles[$collname] = [System.Collections.ArrayList]@()
}
$csvfiles[$collname].insert(0, @{ repdate = $repdate; fileobj = $csv })

## NATIVE POSH SOLUTION...
if (!$csvfiles.ContainsKey($collname)) {
    $csvfiles[$collname] = @()
}
$csvfiles[$collname] = @{ repdate = $repdate; fileobj = $csv }, `
                       $csvfiles[$collname] | %{$_}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:37:39+00:00Added an answer on June 14, 2026 at 5:37 pm

    You might want to use ArrayList objects instead, as they support insertions at arbitrary locations. For example:

    # C:\> $a = [System.Collections.ArrayList]@(1,2,3,4)
    # C:\> $a.insert(0,6)
    # C:\> $a
    6
    1
    2
    3
    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/ I came to conclusion that using this.href is more
After reading this article, it makes sense to rebase to gather changes from the
After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading this article http://lukast.mediablog.sk/log/?p=155 I decided to use mingw on linux to compile
I was under the impression, after reading this article that it is better to
After reading this blog , I want to try writing a similar program myself.
After reading this article: Using Interfaces in C++ I have decided to use the
After reading this question here: Passing one Dimension of a Two Dimensional Array in
After reading this article I made a point that int () yields 0 because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.