Welcome to MSDN Blogs Sign in | Join | Help

PowerShell Tip: How to “shift” arrays…

It's fairly common in scripts to extract the first element out of an array then "shift" the remaining elements. Perl has a special shift operator for doing this as do many of the UNIX shells. PowerShell, however, doesn't have a shift operator built-in. So how do we shift arrays in PowerShell? Well – we could do it the brute force way and simply copy all of the elements after the first one but that sounds like too much work. A way to make it a bit easier is to use the range operator ('..') and array slices as shown:

 

$first = $oldArray[0]

$oldArray = $oldArray[1 .. ($oldArray.count-1)]

 

But that's still too much typing. In practice, PowerShell's multiple assignment feature is the most effective way to handle this kind of thing. Consider the following array:

 

PS (1) > $a = 1,2,3,4,5

 

We can use multiple assignment to split this array into $first and $rest simply by doing:

 

PS (2) > $first, $rest = $a

 

The first element from the array is placed into $first, the remaining elements are copies into $rest

 

PS (3) > $first

1

PS (4) > $rest

2

3

4

5

 

If we don't care about the first element, then we can use $null instead of $first and the value will simply be discarded.

 

PS (5) > $null, $rest = $rest

PS (6) > $rest

3

4

5

PS (7) >

 

This is actually a pretty handy approach to solving a variety of list restructuring problems. For example, say we have a list that looks like:

 

PS (7) > $list = "one",1,"two",2,"three",3

 

The form of this list is simply a name followed by a value:

 

PS (8) > $list

one

1

two

2

three

3

 

Now I want to "fold" that list into a hashtable as a set of key/value pairs. Here's a one-liner that will do this:

 

PS (9) > $hash = @{} ; while ($list) { $key, $value, $list = $list; $hash[$key]=$value }

 

The first two elements of the list go into $key and $value. The remaining elements are assigned back to $list. This loops until $list is empty. The resulting hash table looks like:

 

PS (10) > $hash

 

Name Value

---- -----

two 2

three 3

one 1

 

-bruce

   

Bruce Payette [MSFT]

Windows PowerShell Tech Lead

   

Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell

Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Windows PowerShell in Action (book): http://manning.com/powershell

 

Published Tuesday, February 06, 2007 2:14 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: PowerShell Tip: How to “shift” arrays…

WOW!

The implecations are mind-boggling.

What does this mean to teh pipeline?

Can we take pop one list and push another?

This can be used for queuing problems I suspect.

Tuesday, February 06, 2007 12:20 AM by Jim V

# re: PowerShell Tip: How to “shift” arrays…

"The first two elements of the list go into $key and $value. The remaining elements are assigned back to $list. This loops until $list is empty. The resulting hash table looks like: "

Hmmm... not quite true is it, or the items would be in the same order as they were in $hash.

Tuesday, February 06, 2007 4:01 AM by MC

# re: PowerShell Tip: How to “shift” arrays…

The hashtable class doesn't preserve the order in which elements are added. This is why the list displayed by dumping the hashtable looks different when compared to the original list.

-bruce [MSFT]

Tuesday, February 06, 2007 6:33 PM by Bruce Payette

# re: PowerShell Tip: How to “shift” arrays…

Ooh, CAR & CDR. Nice. :)

I can feel my LISP days coming back to me...

Tuesday, February 06, 2007 7:52 PM by Rob Farley

# re: PowerShell Tip: How to “shift” arrays…

I think you should have a contact e-mail on your website.  Or maybe you are too busy...

Anyways, I would like to request an opinion if Powershell can accomplish a particular solution.  Can Powershell be used to change the password (or entirely disable the account, or manipulate in general) of the local administrator accounts of computers in an Active Directory domain?

Tuesday, February 06, 2007 11:09 PM by aaron.axvig

# re: PowerShell Tip: How to “shift” arrays…

aaron - What you are asking for actually has nothing to do with Powershell. What you want to do is accomplished via the ADSI provider.

It is exposed in PowerShell (see the PowerShell release notes). It is also exposed to traditional Windows Scripting Host and C/C++ APIs.

//David

Wednesday, February 07, 2007 9:46 PM by David Wang

# re: PowerShell Tip: How to “shift” arrays…

Aaron,

So you want to do something like use AD to list the computers, then go to each computer and change the *local* Admin passwd?

Thursday, February 08, 2007 7:44 AM by Marco Shaw

# re: PowerShell Tip: How to “shift” arrays…

Thanks for this tip. How would we push onto and pop from arrays? This is the same as shifting, but operates on the last value, not the 1st.

Monday, February 19, 2007 6:59 PM by jeremy

# Shifting Powershell Arrays

Monday, February 26, 2007 2:25 PM by Geek Noise

# re: PowerShell Tip: How to “shift” arrays…

actually, in perl, shift/unshift push/pop are not operators - they are functions.  The "shift" operator you are missing however, is a bit-shift operator, which i desperately need at the moment...

Monday, August 06, 2007 11:30 AM by sean

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker